Find objects with missing attributes in Datomic

If I have the following Datomic :

{ :fred :age 42 } { :fred :likes :pizza } { :sally :age 42 } 

How to execute a query for both objects ( :fred and :sally ), returning the attribute :likes :pizza for :fred and an empty value for :sally ?

Request

 [:find ?n ?a ?l :where [?n :age ?a] [?n :likes ?l]] 

returns only :fred 42 :pizza .

+6
source share
3 answers

Datomic has recently been updated with several expression functions available to you in Datomic queries. One of these functions is called get-else , and it allows you to provide a default return value if the attribute does not exist in nature, just as clojure.core/get returns setting the third parameter, if the key is not found.

Thus, using your own example, you will need to modify it like this:

[:find ?n ?a ?l :where [?n :age ?a] [(get-else $ ?n :likes false) ?l]

Unfortunately, you cannot actually make nil default value, since it is not a valid Datomic data type, and Datomic will work if you try, but false should get you where you are going too.

+12
source

Returning a set of objects that may or may not have certain attributes, similar to LEFT JOIN in a relational database.

The approach in datomic is to take two steps: the first query for entities, and then go from there to get the attribute values ​​or nil if the attribute is not specified for this object.

See the mailing list. How do you make a left join in Datomic? with an accompanying gist for example.

+6
source

Can you also try "missing"? P.

Look at this:

http://docs.datomic.com/query.html#missing

+4
source

All Articles