More queries

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.

Attributes

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 :db/ident attribute:

[:find ?attr
 :where
 [?p :person/name]
 [?p ?a]
 [?a :db/ident ?attr]]

This is because attributes are also entities in our database!

Transactions

It's also possible to run queries to find information about transactions, such as:

  • When was a fact asserted?
  • When was a fact retracted?
  • Which facts were part of a transaction?
  • Etc.

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.

Query:[ I give up! ]

Input #1:

Find the names of all people associated with a particular movie (i.e. both the actors and the directors)

Query:[ I give up! ]

Input #1:

Input #2:

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/valueType and :db/cardinality attributes as well as :db/ident.

Query:[ I give up! ]

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.

Query:[ I give up! ]