I have documents in a collection such as :
{
"login":"xxx",
"someAttribute":"someValue",
"subDocs":[
{
"data": "val1",
"subDocNestedArray":["WWW","AAAA"]
},
{
"data": "val2",
"subDocNestedArray":["AAAA"]
},
{
"data": "val3",
"subDocNestedArray":["ZZZ"]
}
]
}
I retrieve all documents that match login="xxx" and subDocNestedArray="AAAA" but want only subDocs matching along other properties of the root document, ex :
{
"login":"xxx",
"someAttribute":"someValue",
"subDocs":[
{
"data": "val1",
"subDocNestedArray":["WWW","AAAA"]
},
{
"data": "val2",
"subDocNestedArray":["AAAA"]
}
]
}
I have tried with :
Aggregation.unwind("$subDocs"),
Aggregation.match(Criteria.where("subDocs.subDocNestedArray").is("AAAA")),
Aggregation.group("$_id")
that results in missing fields
{
"login": null,
"someAttribute": null,
"subDocs":[
{
"data": "val1",
"subDocNestedArray":["WWW","AAAA"]
},
{
"data": "val2",
"subDocNestedArray":["AAAA"]
}
]
}
I did not manage to find the syntax using "redact".
Any clue ?
Make my MongoDB query return filtered results
You can try this:
In this, we have a
$match
stage to filter documents, and then anaddFields
stage, to filter thesubDocs
array. Please test it out.