MongoDB:检查值是否为空或数组是否为空 [英] MongoDB: Check if value is null or the array is empty

查看:155
本文介绍了MongoDB:检查值是否为空或数组是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想匹配所有不包含公司"属性或公司"值为空或空数组的文档.

I would like to match all documents that don't contain the "Company" attribute or where the "Company" value is null or an empty array.

User.find({Company: {$in: [null, [] ]}}, function (err, users) {
      if (err) { throw err; }
      console.log(users.length);

}).then(function(doc) {
      console.log("finish User Company");
});

推荐答案

您可以使用 $or 查询运算符,检查每个条件:

You could use the $or query operator with a check for each condition:

{
  $or: [{
    // Check about no Company key
    Company: {
       $exists: false
    }
  }, {
    // Check for null
    Company: null
  }, {
    // Check for empty array
    Company: {
       $size: 0
    }
  }]
}

这篇关于MongoDB:检查值是否为空或数组是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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