Aggregate array values from different documents

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.

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

Something like:

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

With OFFSET 0 and LIMIT 3.

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

1 Like

Hi @atom_yang, yes.

You can also do

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