在我的模式中,我具有应该为每个创建的实体都存在的属性:base/type
。为了检查是否确实如此,我正在尝试查找缺少的实体:
[:find [?entities ...]
:in $ :where
[(missing? $ ?entities :base/type)]]
不幸的是,这还给了我:
Execution error (Exceptions$IllegalArgumentExceptionInfo) at datomic.error/arg (error.clj:57).
:db.error/insufficient-binding [?entities] not bound in expression clause: [(missing? $ ?entities :base/type)]
应如何构造此查询?
这是因为您的查询过于笼统。如果使用查询API,则where语句中至少需要一个肯定子句。不过,您可以访问原始索引以获取结果。如果您有足够的RAM,则可以:
(def snapshot (d/db connection)) ; your db snapshot
(def all-datoms (d/datoms snapshot :eavt))
(def base-type-id (:db/id (d/entity snapshot :base/type))
(def entities (group-by #(.e %) all-datoms))
(def entities-without-base-type (map
(comp #(into {} %) (partial d/entity snapshot) first)
(filter (fn [[k l]]
(empty? (filter #(= base-type-id (.a %))
l)))
entities)))
(def only-relevant-entities (filter #(not (or (:db/ident %) (:db/txInstant %))) entities-without-base-type))
only-relevant-entities
最后一个过滤器是摆脱属性定义和事务(它们也作为datom存储在db中!)。>>
如果实体太多,则可以使用异步原子API来对datom进行分块。
使用::singer/songs
作为示例属性,这是执行查询的方法: