【问题标题】:MongoDB - $nin invalid operator with aggregateMongoDB - $nin 无效运算符与聚合
【发布时间】:2017-05-21 14:41:18
【问题描述】:

我有一个函数应该返回在给定时间段内创建的用户计数,并按受邀和非受邀分组。在$cond 运算符上,我需要比较字段tkbSponsor 是否不是null 并且不等于example@example.com。如果此条件为真,则该用户被邀请。否则,他没有被邀请。

var countByPeriod = function(req,res) {
    var initialDate = req.body.initialDate;
    var finalDate = req.body.finalDate;

    User.aggregate([
        {
            "$match": {
                "timeStamp": {
                    "$gte": new Date(initialDate),
                    "$lt": new Date(finalDate)
                }
            }
        },
        {
            "$group": {
                "_id": null,
                "total": { "$sum": 1 },
                "invited": {
                    "$sum": {
                        "$cond": [
                            { 
                                "tkbSponsor": {
                                    "$nin": ["example@example.com",null] 
                                }
                            },
                            1,
                            0
                        ]
                    }
                }
            }
        }
    ], (err,result) => {
        if (!err)  {
            if (result.length) res.send(result[0]);
            else res.send({"total": 0,"invited":0});
        } else {
            res.sendStatus(500);
            console.log(err);
        }

    });

};

顺便说一句,这个函数在执行时给我一个错误:

{ [MongoError: invalid operator '$nin']
  name: 'MongoError',
  message: 'invalid operator \'$nin\'',
  ok: 0,
  errmsg: 'invalid operator \'$nin\'',
  code: 15999 }

只是一个观察。我以前使用$cond操作符如下,因为我不需要和null比较:

"$cond": [
           { 
             "$ne": ["$tkbSponsor", "example@example.com"]
           },
           1,
           0
]

而且它有效。但是,现在我还必须比较 tkbSponsor 是否不为 null 并且使用 $nin 是否会给我这个错误。

【问题讨论】:

    标签: node.js mongodb mongoose database


    【解决方案1】:

    将其更改为使用 $and$ifNull 合并为:

    {
        "$cond": [
            { 
                "$and": [
                    { "$ne": ["$tkbSponsor", "example@example.com"] },
                    { 
                        "$ne": [
                            { "$ifNull": [ "$tkbSponsor", null ] },
                            null
                        ] 
                    }
                ]
            }, 1, 0
        ]
    }
    

    或使用$or作为

    {
        "$cond": [
            { 
                "$or": [
                    { "$eq": ["$tkbSponsor", "example@example.com"] },
                    { 
                        "$eq": [
                            { "$ifNull": [ "$tkbSponsor", null ] },
                            null
                        ] 
                    }
                ]
            }, 0, 1
        ]
    }
    

    $ifNull 运算符的存在是通过将“不存在”或空字段替换为 null 值来充当 $exists 运算符以进行评估.

    运行它应该返回正确的结果,因为之前的 revision 只评估 tkbSponsor 字段 exists AND 的值为 null 或 "example@example .com”。

    使用 $ifNull,“不存在”的字段也会被评估,因为操作员会为它们提供空值。

    【讨论】:

    • null比较是否等于使用$exists运算符?查询现在正在运行,但结果不正确。我想与null 的比较结果总是正确的。
    • 您能否编辑您的问题以提供一些用于测试的示例文档并显示您期望从该示例中得到的正确结果?
    猜你喜欢
    • 2016-03-14
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    相关资源
    最近更新 更多