Fork me on GitHub
#xtdb
<
2023-06-22
>
Sagar Vrajalal12:06:29

I am trying to perform a "join-aggregate" Let's say I have these set of documents inserted :

[{:xt/id :farm/a
  :farm/id :farm/a}
 {:xt/id :animal/cow
  :animal/farm :farm/a}
 {:xt/id :animal/donkey
  :animal/farm :farm/a}]
I am able to perform the join by adding these clauses :
[[?farm :farm/id ?fid]
 [?animal :animal/farm ?fid]]
Which gives me :
[{:farm/id :farm/a
  :animal/id :animal/cow}
 {:farm/id :farm/a
  :animal/id :animal/donkey}]
But what I want to achieve is :
{:xt/id :farm/a
 :farm/animals [{:xt/id :animal/cow
                 :animal/farm :farm/a}
                {:xt/id :animal/donkey
                 :animal/farm :farm/a}]}
I can achieve this using reduce but is there a way to do this in XTDB using https://docs.xtdb.com/language-reference/1.23.2/datalog-queries/#find-aggregate or https://docs.xtdb.com/language-reference/1.23.2/datalog-queries/#_custom_aggregates?

tatut13:06:48

not with that key afaict, but

user> (xt/pull (xt/db node) '[:farm/id {:animal/_farm [*]}] :farm/a)
{:farm/id :farm/a,
 :animal/_farm
 ({:animal/farm :farm/a, :xt/id :animal/cow}
  {:animal/farm :farm/a, :xt/id :animal/donkey})}

❤️ 2
Sagar Vrajalal13:06:47

Exactly what I needed. Thank you! Out of curiosity, how do we compare the performance of joins using this syntax vs using :where?

tatut13:06:21

I wouldn’t expect there to be a difference, it is likely using the same attribute index

tatut13:06:55

but I haven’t checked, how that performs with massive amounts of linked items