datomic

Ask questions on the official Q&A site at https://ask.datomic.com!
2021-04-07T05:51:42.204300Z

I'm having trouble with datomic client q api, where the aggregate function count returns an empty list instead of 0 if no items were found. What am I doing wrong here? Thanks in advance

(d/q '[:find (count ?e)
       :where [?e :migration/name "non_existing_name"]]
     db) ;; => [] instead of [[0]]

2021-04-07T12:21:25.204900Z

ended up doing count outside the query

(count (flatten (d/q ...etc)))

favila 2021-04-07T13:06:36.205100Z

Aggregation functions are not called on empty resultsets

favila 2021-04-07T13:08:11.205300Z

The flatten shouldn’t be necessary.

favila 2021-04-07T13:09:15.205500Z

if you want to save the bandwidth of returning all items, you can use 0 as a fallback e.g. (or (ffirst result) 0)

2021-04-07T14:05:01.205700Z

noted, thanks for the tip @favila

2021-04-07T12:21:25.204900Z

ended up doing count outside the query

(count (flatten (d/q ...etc)))

favila 2021-04-07T13:06:36.205100Z

Aggregation functions are not called on empty resultsets

favila 2021-04-07T13:08:11.205300Z

The flatten shouldn’t be necessary.

favila 2021-04-07T13:09:15.205500Z

if you want to save the bandwidth of returning all items, you can use 0 as a fallback e.g. (or (ffirst result) 0)

2021-04-07T14:05:01.205700Z

noted, thanks for the tip @favila