MongoDB filter by nested



我需要从嵌套元素中过滤mongoDB中基于多个属性的数据

我的文档:

item: {
items: [
id: 1
prop: "asd",
elems: [
{
id: 111,
isActive: true
},
{
id: 222,
isActive: true          
}
]
]

{
item: {
items: [
id: 2
prop: "asd",
elems: [
{
id: 111,
isActive: false
},
{
id: 444,
isActive: true          
}
]
]

如果我发送id 111,我需要获得所有元素包含id 111但isActive为true的项目

db.collection.find({
items: {
"$elemMatch": {
"elems": {
"$elemMatch": {
id: 111,
isActive: true
}
}
}
}
})

示例:https://mongoplayground.net/p/zl17zmuoe9g

最新更新