{
"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"
}
]
}
在集合中,我需要檢索文檔,其中userList. loginame="[email protected]"
也檢查角色是「admin」的位置。角色是admin的意思是用他們的角色檢索所有userList:LoginName,否則只檢索userList:loginName和他們的角色,它究竟是什麼。Retrive嵌套數組文件
我嘗試這樣做:
db.Site.aggregate([
{ "$match": { "userList.loginName": "[email protected]" } },
{ "$redact": {
"$cond": [
{ "$eq": [
{ "$ifNull" [ "$loginName", "[email protected]" ] },
"[email protected]"
] },
"$$DESCEND",
"$$PRUNE"
]
} }
])
我需要這樣的
{
"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'爲'venkat @ gmail.com'文件?爲什麼不試試'db.Site.find({「userList.loginName」:「[email protected]」})'? – Veeram
yes.loginName as [email protected] – venkat