MongoDB的:找到的所有文件,其中至少有一个数组元素不匹配? [英] Mongodb: Find all documents where at least one array element does not matches?
问题描述
我对猫鼬定义为这个一组文档:
I have a group document defined as this in Mongoose:
var GroupSchema = new Schema({
name: String,
sections: [{type: ObjectId}]
});
如可见的,组中包含的部分的阵列。我也有OBJECTID的名为 archived_sections的另一个数组
我要查找其至少一个部分是不是在archived_sections阵列中的所有群体。那怎么办?
I want to find all groups whose at least one section is NOT in archived_sections array. How to do that?
我试图用$万年运营商是这样的:
I was trying to use $nin operator like this:
Group.find({ sections: { $nin: archived_sections }).exec(function(err, groups){
res.send(groups);
});
但是,这是给我只有那些群体的部分字段保存在数组archived_sections相匹配的元素没有元素的数组。
But this is giving me only those groups whose sections field holds an array with NO elements matching an element in the array archived_sections.
我想找到所有群体,其中至少有一个部分是不是在archived_sections阵列。如何实现这一目标?请帮助
I want to find all groups where at LEAST ONE section is NOT in archived_sections array. How to achieve that? Please help
推荐答案
您可以通过包装你的 $宁
在 $ elemMatch
运营商,这样的 $宁
分别适用于部分中的每个元素
而不是元素集合为一组的:
You can do this by wrapping your $nin
in an $elemMatch
operator so that the $nin
is applied separately to each element of sections
instead of the set of elements as a group:
Group.find({ sections: { $elemMatch: { $nin: archived_sections } } })
.exec(function(err, groups){
res.send(groups);
}
);
如果至少有一个元素满足 $ elemMatch
查询,则文档匹配。
If at least one element satisfies the $elemMatch
query, the doc matches.
这篇关于MongoDB的:找到的所有文件,其中至少有一个数组元素不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!