A datom, as described earlier, is the 4-tuple
[eid attr val tx]. So far, we have only asked questions about values and/or entity-ids. It's important to remember that it's also possible to ask questions about attributes and transactions.
For example, say we want to find all attributes that are associated with person entities in our database. We know for certain that
:person/name is one such attribute, but are there others we have not yet seen?
[:find ?attr :where [?p :person/name] [?p ?attr]]
The above query returns a set of entity ids referring to the attributes we are interested in. To get the actual keywords we need to look them up using the
[:find ?attr :where [?p :person/name] [?p ?a] [?a :db/ident ?attr]]
This is because attributes are also entities in our database!
It's also possible to run queries to find information about transactions, such as:
The transaction entity is the fourth element in the datom vector. The only attribute associated with a transaction (by default) is
:db/txInstant which is the instant in time when the transaction was committed to the database.
Here's how we use the fourth element to find the time that "James Cameron" was set as the name for that person entity:
[:find ?timestamp :where [?p :person/name "James Cameron" ?tx] [?tx :db/txInstant ?timestamp]]
What attributes are associated with a given movie.
Find the names of all people associated with a particular movie (i.e. both the actors and the directors)
Find all available attributes, their type and their cardinality. This is essentially a query to find the schema of the database. To find all installed attributes you must use the
:db.install/attribute attribute. You will also need to use the
:db/cardinality attributes as well as
When was the seed data imported into the database? Grab the transaction of any datom in the database, e.g.,
[_ :movie/title _ ?tx] and work from there.