![](/img/trans.png)
[英]MongoDB - Find elements, where field is equal to at least one value at array
[英]Mongodb: Find all documents where at least one array element does not matches?
我在猫鼬中定义了一个小组文档:
var GroupSchema = new Schema({
name: String,
sections: [{type: ObjectId}]
});
可见,组包含一个节数组。 我也有OBJECTID的所谓archived_sections的另一个数组
我想找到所有至少有一部分不在archived_sections数组中的组。 怎么做?
我试图像这样使用$ nin运算符:
Group.find({ sections: { $nin: archived_sections }).exec(function(err, groups){
res.send(groups);
});
但这只是给我的那些组,其sections字段包含一个数组,其中没有元素与数组archived_sections中的元素匹配。
我想找到所有组,其中“至少一个”部分不在archived_sections数组中。 如何实现呢? 请帮忙
您可以通过将$nin
封装在$elemMatch
运算符中来做到这一点,以便将$nin
分别应用于sections
每个元素,而不是作为一组元素进行应用:
Group.find({ sections: { $elemMatch: { $nin: archived_sections } } })
.exec(function(err, groups){
res.send(groups);
}
);
如果至少一个元素满足$elemMatch
查询,则文档匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.