Du kan bruge nedenstående forespørgsel.
Et par ting, jeg har ændret.
1.Ingen $elemMatch
er påkrævet for enkelte kriterier. Brug punktnotation i stedet for.
2. Flyt undergruppens $map
inde i gruppens $map
operatør.
[
{"$match":{
"empId":"19107",
"address.country":"AUS",
"group.primaryGroup.primary":"Y",
"group.subGroup.primarySubGroup.primary":"Y"
}},
{"$project":{
"empId":1,
"mobile":1,
"address":{"$filter":{"input":"$address","as":"d","cond":{"$eq":["$$d.country","AUS"]}}},
"group":{
"$map":{
"input":"$group",
"as":"v",
"in":{
"groupId":"$$v.groupId",
"primaryGroup":{"$filter":{"input":"$$v.primaryGroup","as":"vp","cond":{"$eq":["$$vp.primary","Y"]}}},
"subGroup":{
"$map":{
"input":"$$v.subGroup",
"as":"n",
"in":{
"subGroupId":"$$n.subGroupId",
"primarySubGroup":{"$filter":{"input":"$$n.primarySubGroup","as":"mp","cond":{"$eq":["$$mp.primary","Y"]}}}
}
}
}
}
}
}
}}
]