127

希望执行以下查询:

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

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

4

6 回答 6

238

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

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

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

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

一些可能有帮助的链接:

于 2013-05-13T22:11:14.260 回答
19

$ne

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

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

$nin

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

User.find({ "groups": { "$nin": ['admin', 'user'] } })
于 2018-09-04T02:44:51.113 回答
10

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

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

代替

{email: {$not: /@domain.com/}}
于 2016-07-28T21:09:14.673 回答
1

好的,伙计们,我找到了解决此问题的可能方法。我意识到 Mongo 中不存在连接,这就是为什么首先您需要使用您喜欢的角色查询用户的 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"} 
          })
    });
于 2020-06-09T02:10:32.540 回答
0

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

async function getRegisterUser() {
    return Login.count({"role": { $ne: 'Super Admin' }}, (err, totResUser) => {
        if (err) {
            return err;
        }
        return totResUser;
    })
}
于 2018-10-12T09:43:35.897 回答
0

大家好,我被这个困住了。我有一个对用户的引用的文档配置文件,并且我尝试列出用户 ref 不为空的配置文件(因为我已经在填充期间被 rol 过滤了),但是在谷歌搜索几个小时后我无法弄清楚如何得到这个。我有这个查询:

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
}

提前致谢。

于 2020-06-08T04:37:39.823 回答