我的mongo数据库具有这样的结构。给定父级_id
,例如123
,我如何进行查询以检查项abc
是否在其父级123
中存在?
[
- _id: 123
name: "item 1"
items: [
{
_id: abc,
age: 12,
},
{
_id: efg,
age: 12,
}
]
,
- id: 456
name: "item 2"
items: [
...
]
]
我目前有这个,并且我已经尝试过$elemMatch
,但似乎不起作用。
db.Collection("album").FindOne(context.Background(), bson.M{"_id": parentID})
答案 0 :(得分:0)
不清楚items
是否具有单独的文档,或者它是否是文档中的嵌套数组。无论哪种情况:
如果items
是文档中的嵌套数组,则:
bson.M{"_id":parentID,"items._id":"abc"}
将查找_id
为parentID
并且在_id:"abc"
的元素之一中包含items
的文档。