Fork me on GitHub
#xtdb
<
2022-07-13
>
Ivan Koz18:07:01

What would be a proper way to return names, like in query below but for all :clan/id 's in the database grouped by :clan/id? Expected result {"cid-1" [names], "cid-2" [names]}

'{:find  [name]
  :where [[c :clan/id "cid-1"]
          [c :clan/members p]
          [p :profile/name name]]}

phronmophobic18:07:21

I thought you could just do:

'{:find  [name]
  :where [[c :clan/id ]
          [c :clan/members p]
          [p :profile/name name]]}
Does that not work?

Ivan Koz18:07:40

returns ungrouped result

Ivan Koz18:07:41

i obviously could do :find [c name] and group by first but i'm interested if that possible to do as a query

refset19:07:28

Hey @UH16CGZC2 perhaps this?

'{:find  [(distinct c) name]
  :where [[c :clan/id]
          [c :clan/members p]
          [p :profile/name name]]}

Ivan Koz20:07:11

clan/id is already distinct natural id, not sure how that helps, for now i did it that way

(comment ;; clan member names grouped by clan/id
  (->> (q '{:find  [c name]
            :where [[c :clan/id]
                    [c :clan/members p]
                    [p :profile/name name]]})
       (group-by first)
       (map #(do [(first %) (mapv second (second %))])))) ;; mapv extracts p/name from [c/id p/name] pairs.
Btw Jeremy i've watched your workshop from re:Clojure, great stuff, was really helpful.

🙏 1
refset11:07:41

Ah, I had the distinct on the wrong arg, but this should work for you, I think:

(with-open [n (xt/start-node {})]
    (xt/q (xt/db n) '{:find [a (distinct b)]
                      :in [[ab ...]]
                      :where [[(identity ab) [a b]]]}
          [[1 :a] [1 :b] [2 :c] [3 :a]]))
  ;;=> #{[3 #{:a}] [2 #{:c}] [1 #{:b :a}]}

Ivan Koz19:07:29

yep something like that, i'll dig into how it works tomorrow, ty

🙂 1