1

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?

  • 1
    You're not doing anything wrong. Why not just (first ...) on the result? – alex314159 Mar 24 '22 at 12:53
  • 1
    Does this answer your question? [Equivalent of SQL "limit" clause in Datomic](https://stackoverflow.com/questions/27162566/equivalent-of-sql-limit-clause-in-datomic) – Eugene Pakhomov Mar 24 '22 at 13:03
  • 2
    @alex314159 Because `d/q` can return an arbitrary amount of items, they might not even fit in memory. – Eugene Pakhomov Mar 24 '22 at 13:04
  • Thank you for the replies! @alex314159 , I'm already using the (first ...) solution, but I wanted a direct query to do that, as there is no scenario where I will use more than one result. – Giorgio Rossa Mar 28 '22 at 12:54
  • @EugenePakhomov , thank you for the link, it was not exactly what I needed, but was a good place to look for possible answers. I will post the way I fixed the issue. – Giorgio Rossa Mar 28 '22 at 12:54

2 Answers2

2

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

1

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)
Hatatister
  • 962
  • 6
  • 11