Unnest Array Inside a dynamic key

Hi, I am very new to Couchbase.

I have document structure below:

{
"Id" : "KA001",
"Class" : {
"A" : {
"Strength" : 56,
"Students" : [
{
"Name" : "Jack",
"Gender" : "Male"
},
{
"Name" : "Bliss",
"Gender" : "Female"
}
]
},
"B" : {
"Strength" : 43,
"Students" : [
{
"Name" : "Jake",
"Gender" : "Male"
},
{
"Name" : "Alexa",
"Gender" : "Female"
}
]
}
}
}

Here Dynamic keys are “A” and “B” (meaning it may have different key name in other documents).
Students array inside these dynamic keys , I want to unnest.
Can anyone help/ guide to fetch the fields inside this students array.

Use OBJECT_VALUES() or OBJECT_PAIRS() to UNNEST

SELECT u.name, u.val AS val
FROM default d 
UNNEST OBJECT_PAIRS(d.Class) AS u
WHERE u.val.Strength = 43;
1 Like