Datomic query - find all records (entities) with value

138 views Asked by At

Query:

(d/q '[:find [?e ...] 
       :in $ ?value 
       :where [?e _ ?value]] 
      db "Germany")

returns nothing, while:

(d/q '[:find [?e ...] 
       :in $ ?value 
       :where [?e :country/name ?value]] 
      db "Germany")

returns list of entities as expected.

Shouldn't the _ serve as a wildcard for any attribute name and return everything that holds a value ?

I read this Datomic query: find all entities with some value, but can't figure how do I stick an actual value as a parameter.

Datomic version: datomic-pro-0.9.5966

1

There are 1 answers

0
Sasha On

I figured this dirty, time consuming method, but it does the job:

(defn all-by-value
 [db value]
 (reduce
  (fn [res ident]
    (try
      (->> (d/q '[:find [?e ...] :in $ ?a ?v :where [?e ?a ?v]] db ident value)
           (map #(d/entity db %))
           (concat res))
      (catch Exception _ res)))
  [] (d/q '[:find [?e ...] :where [?e :db/ident]] db)))

Hope some of you will find it useful.