MongoDB $用于嵌套文档的操作员
有一个WFS集合如下:
{
_id:'1',
transitions:
[
{
_id:'11',
checkLists:
[
{
_id:'111',
name:'One',
},
{
_id:'112',
name:'Two',
}
]
}
]
}
我想获得_id的子文件:'111' 我已经尝试了以下代码,但没有按预期工作,它返回了所有第二级嵌套文档而不是正确的对象
db.wfs.findOne(
{ 'transitions.checkLists._id': ObjectId('111') },
{ 'transitions.checkLists._id': 1 },
);
结果:
{
transitions: [
{
"checkLists": [
{
"name": "One",
"_id": "111"
},
{
"name": "Two",
"_id": "112"
}
]
}
]
}
预期结果:
[
{
"checkLists": [
{
"name": "One",
"_id": "111"
}
]
}
]
赞赏任何提示或解决方案
there is a wfs collection as follows:
{
_id:'1',
transitions:
[
{
_id:'11',
checkLists:
[
{
_id:'111',
name:'One',
},
{
_id:'112',
name:'Two',
}
]
}
]
}
I would like to get the sub sub document of _id:'111'
I have tried the following code but not working as expected, it returns all of 2nd level nested documents not the proper object
db.wfs.findOne(
{ 'transitions.checkLists._id': ObjectId('111') },
{ 'transitions.checkLists._id': 1 },
);
result:
{
transitions: [
{
"checkLists": [
{
"name": "One",
"_id": "111"
},
{
"name": "Two",
"_id": "112"
}
]
}
]
}
Expected result:
[
{
"checkLists": [
{
"name": "One",
"_id": "111"
}
]
}
]
appreciated any hint or solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用:
如此游乐场示例。
$降低
用于“弄平”您的列表,$ filter
用于仅保留所需的零件。这是基于 @rickhg12hs < /a>基本上相似但更复杂的问题。这里的解决方案只是一个简单的版本。
You can use:
As you can see on this playground example.
The
$reduce
is used to "flatten" your list, and the$filter
is used to keep only the part you want.This is based on this solution by @rickhg12hs to a basically similar, but little more complex, problem. The solution here is just a simple version of it.