I am trying to write a query for Cosmos DB which would involve filtering nested arrays. I have a schema similar to
{
"studentname": {
"first": "John",
"last": "Doe",
},
"information":{
"address":[
{
"county" : "Derby",
"street": [
{
"unit" : "123",
"name" : "abc"
},
{
"unit" : "098",
"name" : "efs"
}
]
},
{
"county" : "Essex",
"street": [
{
"unit" : "456",
"name" : "zzz"
},
{
"unit" : "765",
"name" : "aaa"
}
]
}
]
},
"extra": "n/a"
}
I am trying to get:
{
"studentname":{
"first": "John"
},
"information":{
"address":[
{
"street":[
{
"unit": "123"
},
{
"unit": "098"
}
]
},
{
"street":[
{
"unit": "456"
},
{
"unit": "765"
}
]
}
]
}
}
I have tried this query but it does not give desired result
SELECT
{'first': root.studentname.first} as studentname,
{'address': [{'street': ARRAY(SELECT f.unit FROM f IN c.street)}]} as information
FROM root
JOIN c in root.information.address
The output for the query I used is:
{
"studentname": {
"first": "John"
},
"information":{
"address":[
{
"street": [
{
"unit" : "123"
},
{
"unit" : "098"
}
]
}
]
}
},
{
"studentname": {
"first": "John"
},
"information":{
"address":[
{
"street": [
{
"unit" : "456"
},
{
"unit" : "765"
}
]
}
]
}
}
I get the studentname.first and information.address array repeated in the result, which is not required I just want a single information.address array which has the street and unit in it as displayed above.