Hello. I’ve been silently watching these forums for many months, but have finally come across an issue I can’t seem to pass, no matter how much I search for answers.
I have a “team” document (which I specify via USE KEY):
{
"name": "Test",
"members": [{
"role": 3,
"userID": "abc123"
}, {
"role": 1,
"userID": "abcd1234"
}],
"projects": [{
"access": 5,
"projectID": "321cba"
}]
}
Then subsequent documents for “member”:
{
"_id": "abc123",
"name": "Bob",
"password": "abc"
}
{
"_id": "abcd1234",
"name": "Fred",
"password": "cba"
}
And “project”:
{
"_id": "321cba",
"title": "Project 1"
}
I’m trying to make it return it like so:
{
"name": "Test",
"members": [{
"role": 3,
"user": {
"_id": "abc123",
"name": "Bob"
}
}, {
"role": 1,
"user": {
"_id": "abcd1234",
"name": "Fred"
}
}],
"projects": [{
"access": 3,
"project": {
"_id": "abc123",
"title": "Project 1"
}
}]
}
I am able to get one or the other to work, but not together, since it aggregates both sets of arrays together (so if I have 3 members, and 1 project, it’ll actually put 3 duplicate projects instead of 1):
SELECT t.*, ARRAY_AGG({ 'role': tm.`role`, '_id': m.id, 'name': m.name }) members
FROM default t USE KEYS 'teamtest'
UNNEST t.members AS tm
LEFT JOIN default m ON KEYS tm.userID
GROUP BY t
But as soon as I try to unnest “projects”, is when everything goes wrong and I start getting duplicates. I’ve tried LEFT / OUTER joins, unnest, everything. I’ve tried doing LET variables that combine the data… but that only yielded the member / project data… and not the original data that’s part of team (I was losing the member’s “role” field and the project’s “access” field).
Any help would be so much appreciated. I’ve spent hours on this, and I fear that I may have to reconstruct my data layer to make this possible
Thanks ahead of time