【问题标题】:Getting Mongoose Error: Arguments must be aggregate pipeline operators得到 Mongoose 错误:参数必须是聚合管道运算符
【发布时间】:2017-08-24 22:02:59
【问题描述】:

我正在尝试运行此聚合,但无法使用聚合选项参数获得结果,这是怎么回事?这是我的汇总:

 var  coupons = couponModel.aggregate(
        [
            {
                "$lookup": {
                    from: "campaigns",
                    localField: "campaignId",
                    foreignField: "_id",
                    as: "campaigns"
                }
            },
            { "campaignId": { "$in": campaignsIds } },
        ],
        {
            allowDiskUse: true,
            explain: true
        }
    );

也尝试不同的结构:

 couponModel.aggregate(
        [
            {
                "$lookup": {
                    from: "campaigns",
                    localField: "campaignId",
                    foreignField: "_id",
                    as: "campaigns"
                }
            },
            { "campaignId": { "$in": campaignsIds } },
        ],
        function(err,result) {
            if (err) return handleError(err);
            // Result is an array of documents
            console.log(result);
        }
    )

【问题讨论】:

  • 尝试{ $match: { "$in": campaignsIds } } 而不是{ "campaignId": { "$in": campaignsIds } }
  • 感谢@Veeram 成功了!

标签: node.js mongodb mongoose


【解决方案1】:

使用猫鼬时,您不能将对象作为aggregate() 的第二个参数传递。相反,您必须使用猫鼬函数。此外,在管道的第二个对象中,您忘记指定聚合阶段。所以你的代码看起来像这样:

var  coupons = couponModel.aggregate(
        [
            {
                "$lookup": {
                    from: "campaigns",
                    localField: "campaignId",
                    foreignField: "_id",
                    as: "campaigns"
                }
            },
            { $match: { "campaignId": { "$in": campaignsIds } } }
        ]
    )
    .allowDiskUse(true)
    .explain(true);

【讨论】:

    猜你喜欢
    • 2019-03-27
    • 2016-09-17
    • 2014-12-11
    • 2021-06-16
    • 2018-04-24
    • 1970-01-01
    • 2021-06-12
    • 2018-04-22
    • 2017-06-18
    相关资源
    最近更新 更多