如何从数据查询中只找到一条记录?

How to find only one record from query in datomic?

我正在使用 datomic.api 对 datomic 进行查询,如下所示:

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

此查询 return 几乎是预期值,但作为一个数组,如下所示:

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

我希望此查询 return 仅第一个匹配项,而不是所有结果。我做错了什么?

我找到了针对我的具体案例的解决方案。真正的问题是我没有正确理解数据查询。

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

这部分是告诉 datomic 检索多个结果。 我将查询更改为以下查询:

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

而且成功了! 关键是删除 :find 关键字后的“[”,并将“...”切换为仅“.”。

如果这对您不起作用,请查看@EugenePakhomov 在评论中发布的 link:Equivalent of SQL "limit" clause in Datomic

官方有记载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)