在数组的另一个对象中获取数组的猫鼬对象
我在MongoDB系列中有此文档:
"_id": "62be0271d373b2f2fc1826a2",
"condition": "new",
"variants": [
{
"color_name": "Green",
"items": [
{
"size": "S",
"price": 100,
"quantity": 1,
"_id": "62be0271d373b2f2fc1826a4"
},
{
"size": "M",
"price": 100,
"quantity": 2,
"_id": "62be0271d373b2f2fc1826a5"
}
],
"_id": "62be0271d373b2f2fc1826a3"
},
{
"color_name": "Blue",
"items": [
{
"size": "S",
"price": 100,
"quantity": 1,
"_id": "62be0271d373b2f2fc1826a7"
},
{
"size": "S",
"price": 100,
"quantity": 1,
"_id": "62be0271d373b2f2fc1826a8"
}
],
"_id": "62be0271d373b2f2fc1826a6"
}
],
"featured": true
我只想获得第一个具有_id =“ 62BE0271D373B2F2F2F2FC1826A3”的变体,还有第二个具有此“ _id”的项目:“ _id”:“ 62be0271d373b2f2f2f2f2f2f2f2f2fc1826aa5”,也不显示其他字段。
注意:集合中有更多类似的对象,因此循环并匹配所有内容并仅检索匹配的字段。
I have this document in my mongodb collection:
"_id": "62be0271d373b2f2fc1826a2",
"condition": "new",
"variants": [
{
"color_name": "Green",
"items": [
{
"size": "S",
"price": 100,
"quantity": 1,
"_id": "62be0271d373b2f2fc1826a4"
},
{
"size": "M",
"price": 100,
"quantity": 2,
"_id": "62be0271d373b2f2fc1826a5"
}
],
"_id": "62be0271d373b2f2fc1826a3"
},
{
"color_name": "Blue",
"items": [
{
"size": "S",
"price": 100,
"quantity": 1,
"_id": "62be0271d373b2f2fc1826a7"
},
{
"size": "S",
"price": 100,
"quantity": 1,
"_id": "62be0271d373b2f2fc1826a8"
}
],
"_id": "62be0271d373b2f2fc1826a6"
}
],
"featured": true
I want to get only the first variant with it's _id = "62be0271d373b2f2fc1826a3" and also with it's second items that has this "_id": "62be0271d373b2f2fc1826a5", don't show other fields.
NOTE: there are more objects like this in the collection so loop through and match all and retrieve only fields matched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个选项是使用
$ filter
:请参阅 Playground示例
One option is using
$filter
:See how it works on the playground example