I am trying to create a projection where I can take just part of a Array element.
Take this document as a model:
{
"city_info": {
"name": "First City"
"initials": "FC"
},
"postal_codes": {
"ranges": [
{
"name": "Range 1",
"details": "More details",
"another_object": {
(...)
},
"codes": [
{"code": 1},
{"code": 2},
{"code": 3}
]
},
{
"name": "Range 2",
"details": "More details 2",
"another_object": {
(...)
}
"codes": [
{"code": 4},
{"code": 5}
]
}
]
}
}
My query would look like {"postal_codes.ranges.codes.code": 3}
Ranges and codes can have hundreads of elements. "another_object" is just a placeholder used for example.
The expected return would be like:
{
"city_info": {
"name": "First City"
"initials": "FC"
},
"range": {
"name": "Range 1",
"details": "More details",
"another_object": {
(...)
}
}
}
In short, I need to get the array element that matches the query too, but do not return it entirely.
It seems that find is not powerfull enough, I would need some kind of aggregation. I have tried to use a match to return only the documents that match the query, but I do not know how to project only part of the array.
"codes": [ "code": 4, "code": 5 ]should be"codes": [{ "code": 4, "code": 5 }]. Isn't?