Mongoose查询值不为空的内容

141

希望进行以下查询:

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

我是否正确使用了where子句?我想选择那些pincode不为空的文档。

6个回答

271

如果您正在使用查询 API,应该可以像这样完成:

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

这将产生一个类似于mongo查询的查询:

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

以下是几个有用的链接:


5
不相等 (https://www.merriam-webster.com/dictionary/not%20equal) - numbers1311407
MongoDB的相关文档在这里: http://docs.mongodb.org/manual/reference/operator/query/ 关于最新的文档,请参考这里:http://mongoosejs.com/docs/api.html#query_Query-ne - zeropaper
如何使用数组实现,例如 ...("myArraySubDoc[0].someValue").ne(true) - Stephan Kristyn
@SirBenBenji 类似于 where("myArraySubDoc.0.someValue").ne(true) - numbers1311407
我想在一个数组中搜索,如果不存在,则执行特定任务。现在,请问你能告诉我如何做到这一点吗? - aman verma

23

$ne

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

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

$nin

$nin会选择字段值不在指定数组中或者该字段不存在的文档。

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

10

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

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

取代

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

只是一句话,这正是我自己在寻找的,谢谢! - Cacoon
我一直在努力寻找API文档中的$not!谢谢! - Jay Edwards

1

大家好,我找到了一个可能的解决方案。我意识到Mongo中不存在连接(join),这就是为什么您需要首先查询具有所需角色的用户ID,然后再对配置文件文档进行另一个查询,类似于以下内容:

    const exclude: string = '-_id -created_at -gallery -wallet -MaxRequestersPerBooking -active -__v';

  // Get the _ids of users with the role equal to role.
    await User.find({role: role}, {_id: 1, role: 1, name: 1},  function(err, docs) {

        // Map the docs into an array of just the _ids
        var ids = docs.map(function(doc) { return doc._id; });

        // Get the profiles whose users are in that set.
        Profile.find({user: {$in: ids}}, function(err, profiles) {
            // docs contains your answer
            res.json({
                code: 200,
                profiles: profiles,
                page: page
            })
        })
        .select(exclude)
        .populate({
            path: 'user',
            select: '-password -verified -_id -__v'
            // group: { role: "$role"} 
          })
    });

0

统计文档总数,其中字段值不等于指定值。

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

0
大家好,我在这个问题上卡住了。我有一个文档配置文件,其中包含对用户的引用,并且我已经尝试列出用户引用不为空的配置文件(因为我已经在填充期间进行了角色过滤),但是在谷歌几个小时后,我仍然无法弄清楚如何做到这一点。我有以下查询:

const profiles = await Profile.find({ user: {$exists: true,  $ne: null }})
                            .select("-gallery")
                            .sort( {_id: -1} )
                            .skip( skip )
                            .limit(10)
                            .select(exclude)
                            .populate({
                                path: 'user',
                                match: { role: {$eq: customer}},
                                select: '-password -verified -_id -__v'
                              })

                            .exec();

And I get this result, how can I remove from the results the user:null colletions? . I meant, I dont want to get the profile when user is null (the role does not match).
{
    "code": 200,
    "profiles": [
        {
            "description": null,
            "province": "West Midlands",
            "country": "UK",
            "postal_code": "83000",
            "user": null
        },
        {
            "description": null,

            "province": "Madrid",
            "country": "Spain",
            "postal_code": "43000",
            "user": {
                "role": "customer",
                "name": "pedrita",
                "email": "myemail@gmail.com",
                "created_at": "2020-06-05T11:05:36.450Z"
            }
        }
    ],
    "page": 1
}

提前致谢。


1
你不应该以回答的形式提出问题。 - Yasin Okumuş
这是一种解决方法,下一步将是发布答案而不是问题。 - Richard Muvirimi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接