MongoDB的:找到的所有文件,其中至少有一个数组元素不匹配? [英] Mongodb: Find all documents where at least one array element does not matches?

查看:214
本文介绍了MongoDB的:找到的所有文件,其中至少有一个数组元素不匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对猫鼬定义为这个一组文档:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆