Aggregate array values from different documents


#1

Hello.

I’m trying to output one single array merging two arrays from different documents and i can’t figure out how to do it.

Below is an example of what i’m trying to achieve:
Select all followers and followings users to a single array with the possibility to apply LIMIT and OFFSETS.

Document user_account::2::followers:

{
  "followers" : [
    "user_account::1",
    "user_account::3"
  ]
}

Document user_account::2::followings:

{
  "followings" : [
    "user_account::4",
    "user_account::5"
  ]
}

Any idea? I will really appreciate it. Thank you.


#2

can you give us an example of output JSON document,and how apply LIMIT and OFFSETS?


#3

Something like:

{
  "connections" : [
    "user_account::1"
    "user_account::3"
    "user_account::4",
  ]
}

With OFFSET 0 and LIMIT 3.


#4

try this:

SELECT ARRAY_SORT(ARRAY_CONCAT(fs.followers,fi.followings)[0:3])  AS connections
  FROM default AS fs 
  JOIN default AS fi ON KEYS (SPLIT(META(fs).id,"::follow")[0] || "::followings")
 WHERE fs.followers IS NOT MISSING


#5

Hi @atom_yang, yes.

You can also do

(
SELECT ...
UNNEST followers ...
)
UNION
(
SELECT ...
UNNEST followings ...
)
ORDER BY ...
LIMIT ...