{
"id" : "Sir3GHMQ",
"name" : "Medavakkam",
"userList" : [
{
"loginName" : "[email protected]",
"role" : "ADMIN"
},
{
"loginName" : "[email protected]",
"role" : "Operator"
}
]
}
{
"id" : "Sir3GHER",
"name" : "Medavakkam",
"userList" : [
{
"loginName" : "[email protected]",
"role" : "OPERATOR"
},
{
"loginName" : [email protected]",
"role" : "OPERATOR"
}
]
}
In the collection I need to retrieve documents where userList. loginame="[email protected]" also check where role is "admin". Where role is admin means retrieve all userList:LoginName with their role, else retrieve only userList:loginName with their role, what ever it is.
I tried this:
db.Site.aggregate([
{ "$match": { "userList.loginName": "[email protected]" } },
{ "$redact": {
"$cond": [
{ "$eq": [
{ "$ifNull" [ "$loginName", "[email protected]" ] },
"[email protected]"
] },
"$$DESCEND",
"$$PRUNE"
]
} }
])
i need output like this
{
"id" : "Sir3GHMQ",
"name" : "Medavakkam",
"userList" : [
{
"loginName" : "[email protected]",
"role" : "ADMIN"
},
{
"loginName" : "[email protected]",
"role" : "Operator"
}
]
}
{
"id" : "Sir3GHER",
"name" : "Medavakkam",
"userList" : [
{
"loginName" : "[email protected]",
"role" : "OPERATOR"
}
]
}
loginNameas[email protected]? Why not trydb.Site.find({"userList.loginName": "[email protected]"})?