Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB aggregate for Arithmetic Operations - Subdocument Field

We have a aggregate query projecting few sub documents. We wanted to apply a few arithmetic operations like Sum and Product on these projected values.

Aggregation Query --

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);
        }
    });

This gives an error stating invalid operator '$sum'

What should we do to get the sum of original_y and new_y in the $project?

EDIT

Documents:

{
 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},
  ]
}

OUTPUT EXPECTED:

 {
     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},
      ]
    }

where, tallyAmount = (original_y + new_y) / cost

ERROR: can't add an expression for a subfield of dummy because there is already an expression that applies to the whole field

like image 396
Rohit Lala Avatar asked May 04 '15 14:05

Rohit Lala


People also ask

How do I add a field in MongoDB aggregate?

You can include one or more $addFields stages in an aggregation operation. To add field or fields to embedded documents (including documents in arrays) use the dot notation. See example. To add an element to an existing array field with $addFields , use with $concatArrays .

Which aggregate method is preferred for use by MongoDB?

The aggregate() Method For the aggregation in MongoDB, you should use aggregate() method.


1 Answers

Without much details of your document schema and the expected aggregation result, I would suggest you try the following aggregation as I believe you need the $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 operator:

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);

-- UPDATE --

To satisfy the condition tallyAmount = (original_y + new_y) / cost, you should use the $add and $divide arithmetic operators in your $project operator pipeline stage thus your final aggregation pipeline would look like this:

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);
like image 136
chridam Avatar answered Oct 21 '22 08:10

chridam