MongoDB 算术运算的聚合 - 子文档字段

MongoDB aggregate for Arithmetic Operations - Subdocument Field

我们有一个聚合查询投影几个子文档。我们想对这些投影值应用一些算术运算,例如 Sum 和 Product。

聚合查询 --

Item.aggregate([
        { $unwind: '$dummy'},       
        { $match: {'dummy.storage': {$gt: 0}} },
        { $group: {_id: '$_id',
                    dummy: {$push: '$dummy'},
                    original_y: { $first: "$original_y" },
                    new_y: { $first: "$new_y" },

        }},
        {$project:{
                  original_y: 1, new_y: 1,
                  tallyAmount: {$sum: ["$new_y","$original_y"] }
                }
        },
     ]
    )
    .exec(function(err, results){   
        if(err)
        {
            console.log("Error : " + err);
            return res.json ({error: "Error"});

        }
        else if(!(results) || results == null || results.length == 0)
        {
            console.log("No Results Found");
            return res.json ({error: "No Results Today"});

        }else{

            res.send(results);
        }
    });

这给出了一个错误说明 invalid operator '$sum'

我们应该怎么做才能得到$project中的original_ynew_y

编辑

文件:

{
 id:1,
 original_y: 200,
 new_y: 140,
 dummy: [
  {id:1, storage:2, cost: 10},
  {id:2, storage:0, cost: 20},
  {id:3, storage:5, cost: 30},
  ]
}

预期输出:

 {
     id:1,
     original_y: 200,
     new_y: 140,
     dummy: [
      {id:1, storage:2, cost: 10, tallyAmount: 34},
      {id:3, storage:5, cost: 30, tallyAmount: 11.33},
      ]
    }

其中, tallyAmount = (original_y + new_y) / cost

错误:无法为虚拟子字段添加表达式,因为已经有一个表达式适用于整个字段

没有太多关于文档架构和预期聚合结果的详细信息,我建议您尝试以下聚合,因为我相信您需要 $add operator rather than the $sum operator. Note that the $sum operator is only applicable to the $group operator. With the $add operator, it adds up two numbers/fields together and the result is stored in a new field with the $project 运算符:

Item.aggregate([
    { "$match": { "dummy.storage": { "$gt": 0 } } },
    { "$unwind": "$dummy" },   
    { "$group": { 
        "_id": "$_id", 
        "original_y": { "$first": "$original_y" },
        "new_y": { "$first": "$new_y" }
    } },
    { "$project": {
        "original_y": 1, 
        "new_y": 1,
        "tallyAmount": { "$add": [ "$new_y", "$original_y" ] }
    } }
]).exec(callback);

-- 更新 --

要满足条件 tallyAmount = (original_y + new_y) / cost,您应该在 $project 运算符管道阶段使用 $add and $divide 算术运算符,因此您最终的聚合管道将如下所示:

Item.aggregate([
    { "$match": { "dummy.storage": { "$gt": 0 } } },
    { "$unwind": "$dummy" },
    {
        "$project": {
            "original_y": 1, 
            "new_y": 1,
            "dummy.id": "$dummy.id",
            "dummy.storage": "$dummy.storage",
            "dummy.cost": "$dummy.cost",
            "dummy.tallyAmount": {
                "$divide": [
                    { "$add": ["$new_y","$original_y"] },
                    "$dummy.cost"
                ]
            }
        }
    },
    {
        "$group": {
            "_id": "_$id",
            "original_y": { "$first": "$original_y" },
            "new_y": { "$first": "$new_y" },
            "dummy": {
                "$addToSet": "$dummy"
            }
        }        
    }
 ]).exec(callback);