How to find only one record from query in datomic?

313 Views Asked by At

I'm doing a query on datomic using datomic.api like the following:

(d/q
  '[:find [(pull ?a [*]) ...]
    :in $ ?title
    :where
    [?a :movie/title ?title]]
 db title)

This query is returning almost the expected value, but as an array, like this:

[ {:db/id 17592186045442, :movie/title "Test", :movie/year 1984, :movie/director #:db{:id 17592186045439 }} ]

I want this query to return only the first match, and not all the results. What I'm doing wrong?

2

There are 2 best solutions below

1
Hatatister On BEST ANSWER

It is documented in the official Datomic documentation:

Find Spec

  • :find ?a ?b relation (Collection of Lists)
  • :find [?a …] collection (Collection)
  • :find [?a ?b] single tuple (List)
  • :find ?a . single scalar (Scalar Value)
0
Giorgio Rossa On

I found a solution for my specific case. The real issue was that I was not understanding the datomic query correctly.

[:find [(pull ?a [*]) ...]

This part is telling datomic to retrieve more than one result. I changed the query to the following one:

(d/q
  '[:find (pull ?a [*]) .
    :in $ ?title
    :where
    [?a :movie/title ?title]]
 db title)

And it worked! The key thing was to remove the "[" after :find keyword, and switch the "..." for only ".".

If this doesn't work for you, look on the link that @EugenePakhomov posted on the comments: Equivalent of SQL "limit" clause in Datomic