Fork me on GitHub
#xtdb
<
2020-08-16
>
nivekuil04:08:05

when passing in a vector to :args, can crux return the results in the same order as :args? or is this best done in application logic

jarohen11:08:51

the ordering of Crux query results for a query without an explicit order-by is undefined, but you could include an index variable in the args, and then order the Crux query by that index:

(crux/submit-tx node
                [[:crux.tx/put {:crux.db/id :doc-a}]
                 [:crux.tx/put {:crux.db/id :doc-b}]
                 [:crux.tx/put {:crux.db/id :doc-c}]
                 [:crux.tx/put {:crux.db/id :doc-d}]
                 [:crux.tx/put {:crux.db/id :doc-e}]])

(crux/q db '{:find [?idx ?eid]
             :where [[?e :crux.db/id ?eid]]
             :order-by [[?idx]]
             :args [{?idx 0, ?eid :doc-c}
                    {?idx 1, ?eid :doc-b}
                    {?idx 2, ?eid :doc-e}
                    {?idx 3, ?eid :doc-a}]})
;; => [[0 :doc-c] [1 :doc-b] [2 :doc-e] [3 :doc-a]]

nivekuil11:08:17

thanks, that's pretty elegant. Is [?e :crux.db/id ?eid] the canonical way to do this kind of query where you already know the entity? I've been using [?e :crux.db/id ￱_￱] and I _think_ that works the same..

jarohen16:08:59

there isn't really a canonical way, tbh, they're all equivalent - even [?e :crux.db/id ?e] 🙂