记录::
{
"_id" : ObjectId("5526040fc58e95b598cf1bc6"),
"group" : [
[
{
"userId" : "551b71dbb47628dd9e2882e0",
"socketid" : "",
"group_name" : "A1",
"un" : "robot20",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90",
"is_robot" : true
},
{
"userId" : "551b71ddb47628dd9e2882e1",
"socketid" : "",
"group_name" : "A2",
"un" : "robot21",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90",
"is_robot" : true
}
],
[
{
"userId" : "551cd7656c55761724e3a0e0",
"socketid" : "cInGsNcr9G9OMvVyAAAA",
"group_name" : "B1",
"un" : "Hardik Ranapariya",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90"
},
{
"userId" : "551b71deb47628dd9e2882e2",
"socketid" : "",
"group_name" : "B2",
"un" : "robot22",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90",
"is_robot" : true
}
],
[
{
"userId" : "551b71e0b47628dd9e2882e3",
"socketid" : "",
"group_name" : "C1",
"un" : "robot23",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90",
"is_robot" : true
},
{
"userId" : "551b71e1b47628dd9e2882e4",
"socketid" : "",
"group_name" : "C2",
"un" : "robot24",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90",
"is_robot" : true
}
],
[
{
"userId" : "551b71e3b47628dd9e2882e5",
"socketid" : "",
"group_name" : "D1",
"un" : "robot25",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90",
"is_robot" : true
},
{
"userId" : "551b71e8b47628dd9e2882e8",
"socketid" : "",
"group_name" : "D2",
"un" : "robot28",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90",
"is_robot" : true
}
]
],
"group2" : [
[
{
"userId" : "551b71ddb47628dd9e2882e1",
"socketid" : "",
"group_name" : "A2",
"un" : "robot21",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90",
"is_robot" : true
}
],
[
{
"userId" : "551b71e1b47628dd9e2882e4",
"socketid" : "",
"group_name" : "C2",
"un" : "robot24",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90",
"is_robot" : true
},
{
"userId" : "551b71e8b47628dd9e2882e8",
"socketid" : "",
"group_name" : "D2",
"un" : "robot28",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90",
"is_robot" : true
}
]
],
"group3" : [
{
"userId" : "551b71e1b47628dd9e2882e4",
"socketid" : "",
"group_name" : "C2",
"un" : "robot24",
"pp" : "https://graph.facebook.com/347571208785861/picture?height=90&width=90",
"is_robot" : true
}
],
"status" : 1
}
我希望在组数组中使用userId
找到此记录。
这里,userId是数组中的数组,然后是对象。 所以请帮我解决这个问题。
我也尝试以下查询来查找
db.test.find({'group.user':'551b71dbb47628dd9e2882e0'})
但它不起作用。
提前感谢..
答案 0 :(得分:3)
要查找嵌套的数组文档数组,请使用mongo $elemMatch。查询如下
db.collectionName.find({"group":{"$elemMatch":
{"$elemMatch":{"userId":"551b71dbb47628dd9e2882e0"}}}},
{"group.$":1}
).pretty()
答案 1 :(得分:0)
拼写错误,
db.test.find({'group.userId':'551b71dbb47628dd9e2882e0'})
user =>用户ID。
你还可以进行“$ elemMatch”操作:
db.test.find (
{group:
{ $elemMatch: {userId:"551b71dbb47628dd9e2882e0"}}
},
{"group.$":1}
})
答案 2 :(得分:0)
由于它涉及查询嵌套数组元素,如果你知道数组的索引,可以使用下面的代码。
注意:运算符不能用于遍历多个数组的查询(See for more)
db.test.find({"group": {"$elemMatch": {"0.userId": "551b71dbb47628dd9e2882e0"}}})