Hi,
your response was awesome and allowed me to get one step closer to what I want the query to achieve, but now I am running into another issue and can’t find a way around it, but I am sure there is one. Here is the structure of the document:
{
"boats": [
{
"boatid": "boat::L4tdDXPxyTyDrqQJRMWTUP",
"captainid": "user::p4zMjxu6DvdRT3NZxyKm2E",
"crews": [
{
"checkedin": false,
"email": "member-HmESvYQf@foo.com",
"name": "member-HmESvYQf",
"userid": "user::E9QJDzktNTSnTYm9W5aSrg"
},
{
"checkedin": false,
"email": "member-mwTXtIBc@foo.com",
"name": "member-mwTXtIBc",
"userid": "user::a5AczU4r22zWvhQcndSdhZ"
}
],
"sail": "fvYGZ637"
},
{
"boatid": "boat::cBwGp93NCPja2hHHuKSgFJ",
"captainid": "user::LiZNrnif5zw2HXwAuhzTZm",
"crews": [
{
"checkedin": false,
"email": "member-vQOmSlEd@foo.com",
"name": "member-vQOmSlEd",
"userid": "user::dCwUEgMcRK5LbwsFQ2vC4"
}
],
"sail": "oySqh3563"
},
{
"boatid": "boat::iNYXs2w4dcNCYu8KSNrnzW",
"captainid": "user::Kcc9tMGXFG2FBPAE6BocvS",
"crews": [
{
"checkedin": false,
"email": "member-NwpJnLhF@foo.com",
"name": "member-NwpJnLhF",
"userid": "user::2pbFimET4wrZgBr24SQxBM"
}
],
"sail": "SOijH4961"
}
],
"date": "2021-03-12",
"fleets": [
{
"fleet": "J/24",
"launchtime": "18:00"
},
{
"fleet": "Spin A",
"launchtime": "18:10"
}
],
"index": 1,
"numboats": 3,
"racecommittee": [
{
"email": "member1@foo.com"
},
{
"email": "sailor1@foo.com"
}
],
"raceid": "1",
"raceseriesid": "raceseries::1",
"type": "race",
"yachtclubid": "yachtclub::1"
}
and I want to return the following document:
[
{
"boats": [
{
"boatid": "boat::L4tdDXPxyTyDrqQJRMWTUP",
"captainid": "user::p4zMjxu6DvdRT3NZxyKm2E",
"boatname": "HippyBoat-#10",
"captain": "captain-AyzaZWan",
"fleet": "Melges",
"phrf": 765,
"crews": [
{
"checkedin": false,
"email": "member-HmESvYQf@foo.com",
"name": "member-HmESvYQf",
"userid": "user::E9QJDzktNTSnTYm9W5aSrg"
},
{
"checkedin": false,
"email": "member-mwTXtIBc@foo.com",
"name": "member-mwTXtIBc",
"userid": "user::a5AczU4r22zWvhQcndSdhZ"
}
],
"sail": "fvYGZ637"
},
{
"boatid": "boat::cBwGp93NCPja2hHHuKSgFJ",
"captainid": "user::LiZNrnif5zw2HXwAuhzTZm",
"boatname": "HippyBoat-#11",
"captain": "captain-uGxrfdVQ",
"fleet": "Melges",
"phrf": 175,
"crews": [
{
"checkedin": false,
"email": "member-vQOmSlEd@foo.com",
"name": "member-vQOmSlEd",
"userid": "user::dCwUEgMcRK5LbwsFQ2vC4"
}
],
"sail": "oySqh3563"
},
{
"boatid": "boat::iNYXs2w4dcNCYu8KSNrnzW",
"captainid": "user::Kcc9tMGXFG2FBPAE6BocvS",
"boatname": "HippyBoat-#9",
"captain": "captain-NrMJAHhx",
"fleet": "J/24",
"phrf": 630,
"crews": [
{
"checkedin": false,
"email": "member-NwpJnLhF@foo.com",
"name": "member-NwpJnLhF",
"userid": "user::2pbFimET4wrZgBr24SQxBM"
}
],
"sail": "SOijH4961"
}
],
"date": "2021-03-12",
"fleets": [
{
"fleet": "J/24",
"launchtime": "18:00"
},
{
"fleet": "Spin A",
"launchtime": "18:10"
}
],
"index": 1,
"numboats": 3,
"racecommittee": [
{
"email": "member1@foo.com"
},
{
"email": "sailor1@foo.com"
}
],
"raceid": "1",
"raceseriesid": "raceseries::1",
"rclist": [
{
"email": "member1@foo.com",
"name": "member1"
},
{
"email": "sailor1@foo.com",
"name": "sailor1"
}
],
"type": "race",
"yachtclubid": "yachtclub::1"
}
]
Here is the query I am using:
SELECT race.*,
ARRAY_AGG(OBJECT_ADD(committee, 'name', member.name)) AS rclist,
ARRAY_AGG({"boatname": boat.name, "fleet": boat.fleet, "phrf": boat.phrf, "captain": captain.name, "boatid": raceboat.boatid, "crews": raceboat.crews, "sail": boat.sail}) AS raceboats
FROM railmeat AS race
UNNEST race1.racecommittee AS committee
UNNEST race1.boats AS raceboat
JOIN railmeat AS member ON committee.email = member.email
JOIN railmeat AS boat ON raceboat.boatid = META(boat).id
JOIN railmeat AS captain ON raceboat.captainid = META(captain).id
WHERE race.raceid='1'
AND race.type='race'
GROUP BY race
Your answer was awesome as it allowed me to add the right elements to my object, but when I run the query, this is what I get:
[
{
"boats": [
{
"boatid": "boat::L4tdDXPxyTyDrqQJRMWTUP",
"captainid": "user::p4zMjxu6DvdRT3NZxyKm2E",
"boatname": "HippyBoat-#10",
"captain": "captain-AyzaZWan",
"fleet": "Melges",
"phrf": 765,
"crews": [
{
"checkedin": false,
"email": "member-HmESvYQf@foo.com",
"name": "member-HmESvYQf",
"userid": "user::E9QJDzktNTSnTYm9W5aSrg"
},
{
"checkedin": false,
"email": "member-mwTXtIBc@foo.com",
"name": "member-mwTXtIBc",
"userid": "user::a5AczU4r22zWvhQcndSdhZ"
}
],
"sail": "fvYGZ637"
},
{
"boatid": "boat::L4tdDXPxyTyDrqQJRMWTUP",
"captainid": "user::p4zMjxu6DvdRT3NZxyKm2E",
"boatname": "HippyBoat-#10",
"captain": "captain-AyzaZWan",
"fleet": "Melges",
"phrf": 765,
"crews": [
{
"checkedin": false,
"email": "member-HmESvYQf@foo.com",
"name": "member-HmESvYQf",
"userid": "user::E9QJDzktNTSnTYm9W5aSrg"
},
{
"checkedin": false,
"email": "member-mwTXtIBc@foo.com",
"name": "member-mwTXtIBc",
"userid": "user::a5AczU4r22zWvhQcndSdhZ"
}
],
"sail": "fvYGZ637"
},
{
"boatid": "boat::cBwGp93NCPja2hHHuKSgFJ",
"captainid": "user::LiZNrnif5zw2HXwAuhzTZm",
"boatname": "HippyBoat-#11",
"captain": "captain-uGxrfdVQ",
"fleet": "Melges",
"phrf": 175,
"crews": [
{
"checkedin": false,
"email": "member-vQOmSlEd@foo.com",
"name": "member-vQOmSlEd",
"userid": "user::dCwUEgMcRK5LbwsFQ2vC4"
}
],
"sail": "oySqh3563"
},
{
"boatid": "boat::cBwGp93NCPja2hHHuKSgFJ",
"captainid": "user::LiZNrnif5zw2HXwAuhzTZm",
"boatname": "HippyBoat-#11",
"captain": "captain-uGxrfdVQ",
"fleet": "Melges",
"phrf": 175,
"crews": [
{
"checkedin": false,
"email": "member-vQOmSlEd@foo.com",
"name": "member-vQOmSlEd",
"userid": "user::dCwUEgMcRK5LbwsFQ2vC4"
}
],
"sail": "oySqh3563"
},
{
"boatid": "boat::iNYXs2w4dcNCYu8KSNrnzW",
"captainid": "user::Kcc9tMGXFG2FBPAE6BocvS",
"boatname": "HippyBoat-#9",
"captain": "captain-NrMJAHhx",
"fleet": "J/24",
"phrf": 630,
"crews": [
{
"checkedin": false,
"email": "member-NwpJnLhF@foo.com",
"name": "member-NwpJnLhF",
"userid": "user::2pbFimET4wrZgBr24SQxBM"
}
],
"sail": "SOijH4961"
},
{
"boatid": "boat::iNYXs2w4dcNCYu8KSNrnzW",
"captainid": "user::Kcc9tMGXFG2FBPAE6BocvS",
"boatname": "HippyBoat-#9",
"captain": "captain-NrMJAHhx",
"fleet": "J/24",
"phrf": 630,
"crews": [
{
"checkedin": false,
"email": "member-NwpJnLhF@foo.com",
"name": "member-NwpJnLhF",
"userid": "user::2pbFimET4wrZgBr24SQxBM"
}
],
"sail": "SOijH4961"
}
],
"date": "2021-03-12",
"fleets": [
{
"fleet": "J/24",
"launchtime": "18:00"
},
{
"fleet": "Spin A",
"launchtime": "18:10"
}
],
"index": 1,
"numboats": 3,
"racecommittee": [
{
"email": "member1@foo.com"
},
{
"email": "sailor1@foo.com"
}
],
"raceid": "1",
"raceseriesid": "raceseries::1",
"rclist": [
{
"email": "member1@foo.com",
"name": "member1"
},
{
"email": "member1@foo.com",
"name": "member1"
},
{
"email": "member1@foo.com",
"name": "member1"
},
{
"email": "sailor1@foo.com",
"name": "sailor1"
},
{
"email": "sailor1@foo.com",
"name": "sailor1"
},
{
"email": "sailor1@foo.com",
"name": "sailor1"
}
],
"type": "race",
"yachtclubid": "yachtclub::1"
}
]
where each boat is repeated as many times as the number of elements in the racecommittee
array and each element in rclist is repeated as many times as the number of boats in the boats
array. I understand that the reason for this is that UNNEST
does a join with the parent document and as such results in as many copies of that document as there are elements in the array. I thought this could be fixed with the GROUP BY
clause, but only if I do a single UNNEST
.
What I don’t know if how to fix this.
I have tried removing the UNNEST
and instead do the following query:
SELECT race.*,
ARRAY_AGG(OBJECT_ADD(committee, 'name', member.name)) AS rclist,
ARRAY_AGG({"boatname": boat.name, "fleet": boat.fleet, "phrf": boat.phrf, "captain": captain.name, "boatid": raceboat.boatid, "crews": raceboat.crews, "sail": boat.sail}) AS raceboats
FROM railmeat AS race
JOIN railmeat AS member ON ANY committee IN race.racecommittee SATISFIES committee.email = member.email END
JOIN railmeat AS boat ON ANY raceboat IN race.boats SATISFIES raceboat.boatid = META(boat).id END
JOIN railmeat AS captain ON ANY rboat IN race.boats SATISFIES rboat.captainid = META(captain).id END
WHERE race.raceid='1'
AND race.type='race'
GROUP BY race
but then raceboat
can’t be used to create the object in the SELECT
clause.
How do I need to change the query so that it returns what I need? I feel like I am super close, but that I am totally missing something. Thoughts?
Thanks a lot again for your help. You are absolutely awesome.
Bertrand.