Hi
I’m trying to query on a subdocument field and return only matching subdocuments. I’m not too worried about the structure of the returned data, array or not for example.
Data looks like this where items in groups can have any key:
Topdocument: {
groups: {
subdoc1: {
name: 'my name'
},
subdocXyz: {
name: 'my name'
}
...
}
}
Query so far that does not work, giving error: “FieldPath field names may not start with ‘$’”
db.getCollection('mycollection').aggregate([
{
$project: {
dynamicKeys: { $objectToArray: "$groups" }
}
},
{
$match: {
'dynamicKeys': {
$elemMatch: {
"v.name": /my group name/
}
}
}
},
{
$project: {
$filter: {
input: "$dynamicKeys",
as: "dk",
cond: {
$eq: [ "$$dk.name", "my group name" ]
}
}
}
},
{
$limit: 3
}
])