mongodb 猫鼬查询其中值不为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16531895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:13:43  来源:igfitidea点击:

Mongoose query where value is not null

mongodbmongoose

提问by wesbos

Looking to do the following query:

希望执行以下查询:

Entrant
    .find
      enterDate : oneMonthAgo
      confirmed : true
    .where('pincode.length > 0')
    .exec (err,entrants)->

Am I doing the where clause properly? I want to select documents where pincodeis not null.

我是否正确执行 where 子句?我想选择pincode不为空的文档。

回答by numbers1311407

You should be able to do this like (as you're using the query api):

您应该可以这样做(因为您正在使用查询 api):

Entrant.where("pincode").ne(null)

... which will result in a mongo query resembling:

...这将导致一个 mongo 查询类似于:

entrants.find({ pincode: { $ne: null } })

A few links that might help:

一些可能有帮助的链接:

回答by MalcolmOcean

I ended up here and my issue was that I was querying for

我最终到了这里,我的问题是我正在查询

{$not: {email: /@domain.com/}}

instead of

代替

{email: {$not: /@domain.com/}}

回答by Tính Ng? Quang

$ne

$ne

selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.

选择字段值不等于指定值的文档。这包括不包含该字段的文档。

User.find({ "username": { "$ne": 'admin' } })

$nin

$nin

$nin selects the documents where: the field value is not in the specified array or the field does not exist.

$nin 选择以下文档:字段值不在指定数组中或字段不存在。

User.find({ "groups": { "$nin": ['admin', 'user'] } })

回答by Vadivel Subramanian

total count the documents where the value of the field is not equal to the specified value.

总计统计字段值不等于指定值的文档。

async function getRegisterUser() {
    return Login.count({"role": { $ne: 'Super Admin' }}, (err, totResUser) => {
        if (err) {
            return err;
        }
        return totResUser;
    })
}