谷歌搜索了很多之后,我找不到可以处理这个问题的东西。我想这应该很简单。我有这个简单的 json...
{
"_id" : ObjectId("555bd34329de3cf232434ef2"),
"clients" : [
{
"Client1" : {
"positions" : [
{
"systemId" : "xxx1",
"type" : "alfa"
},
{
"systemId" : "xxx2",
"type" : "bravo"
},
{
"systemId" : "xxx3",
"type" : "charlie"
}
]
},
"Client2" : {
"positions" : [
{
"systemId" : "xxx4",
"type" : "alfa"
},
{
"systemId" : "xxx5",
"type" : "bravo"
},
{
"systemId" : "xxx6",
"type" : "charlie"
}
]
}
}
]
}
我正在尝试基于 {systemId} 执行查询,查询位置数组位于另一个数组内的数组内。我可以轻松地在单级数组中使用 find()。但此时我需要一个额外的深度,我真的面临困难。有人可以帮我吗?
你好!
最佳答案
如果你想找出 Client1.positions
与 systemId
和 Client2.positions
与 systemId
使用以下聚合:
db.collectionName.aggregate([
{
"$unwind": "$clients"
},
{
"$unwind": "$clients.Client1.positions"
},
{
"$unwind": "$clients.Client2.positions"
},
{
"$match": {
"clients.Client1.positions.systemId": "xxx1",
"clients.Client2.positions.systemId": "xxx4"
}
}
]).pretty()
如果你只想找出 Client1
然后删除 "$unwind": "$clients.Client2.positions"
并匹配 "clients.Client2 .positions.systemId": "xxx4"
关于mongodb - 在 MongoDB 中查找 2 级嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30354323/