【问题标题】:Mongoose query where value is not null值不为空的猫鼬查询
【发布时间】:2013-05-08 01:37:52
【问题描述】:

希望进行以下查询:

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

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

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

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

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

    ... 这将导致类似以下的 mongo 查询:

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

    一些可能有帮助的链接:

    【讨论】:

    • “不等于”,添加答案链接
    • 关于它的 mongodb 文档在这里(现在):docs.mongodb.org/manual/reference/operator/query 关于它的最新文档在这里:mongoosejs.com/docs/api.html#query_Query-ne
    • 如何用数组实现,例如...("myArraySubDoc[0].someValue").ne(true) ?
    • @SirBenBenji 类似where("myArraySubDoc.0.someValue").ne(true)
    • 我想在一个数组中搜索,如果它不存在那么我现在想执行一个特定的任务你能告诉我我该怎么做吗??
    【解决方案2】:

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

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

    而不是

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

    【讨论】:

    • 请注意,这正是我自己正在寻找的东西,谢谢!
    • 我很难在 api 文档中找到 $not !谢谢!
    【解决方案3】:

    $ne

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

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

    $nin

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

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        大家好,我对此感到困惑。我有一个对用户的引用的文档配置文件,并且我尝试列出用户 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
        }
        

        提前致谢。

        【讨论】:

        • 你不应该以答案的形式提问
        【解决方案6】:

        好的,伙计们,我找到了解决此问题的可能方法。我意识到 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"} 
                  })
            });
        

        【讨论】:

          猜你喜欢
          • 2018-03-07
          • 2016-07-11
          • 2020-06-02
          • 2014-03-21
          • 1970-01-01
          • 2023-03-21
          • 1970-01-01
          • 2016-01-10
          • 1970-01-01
          相关资源
          最近更新 更多