用猫鼬在Mongodb中嵌套嵌套阵列
我需要过滤MongoDB中的多层嵌套阵列。模式如下,
{
"_id": "1234",
"array1": [
{
"id": "a11",
"array2": [
{
"id": "a21",
"array3": [
{
"id": "a31",
"status": "done"
},
{
"id": "a32",
"status": "pending"
}
]
}
]
}
]
}
所需的输出必须过滤 array3 ,条件状态=完成。哪种最佳方法可以实现同样的方法?
I need to filter a multi-level nested array in MongoDB. The schema is as follows,
{
"_id": "1234",
"array1": [
{
"id": "a11",
"array2": [
{
"id": "a21",
"array3": [
{
"id": "a31",
"status": "done"
},
{
"id": "a32",
"status": "pending"
}
]
}
]
}
]
}
The required output must filter array3 with condition status=done. Which is the best possible method to achieve the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
$ MAP
迭代array1
和array2
,使用$ filter
filterarray3
。最后,将array3
与空数组进行比较,以匹配文档。这是 mongo playground 供您参考。
Use
$map
to iteratearray1
andarray2
, use$filter
to filterarray3
. Finally comparearray3
with empty array for document matching.Here is the Mongo playground for your reference.
查询
$ MERGEOBJECTS
允许我们不手工编写字段(如果MongoDB5+)也可以使用$ SETFIELD(如果代替ID,则您也有10个字段,这将有效不更改查询)playmongo
Query
$mergeObjects
allows us to not write the fields by hand($setField can be used also if mongoDB5+) (if instead of id you also have 10 fields, this will work without changing the query)Playmongo
我只是从
。
让我再次将代码放在这里,因为这更深
mongo Playground
i just asnwered the same question from https://stackoverflow.com/a/75599962/9267467
the query is simply.
let me put my code here again because this is more deeper
MONGO PLAYGROUND