1

I have several numeric fields that I need to aggregate. Let's say my document is structured as follows:

_id: 1234,
numValue1: 10,
numValue2: 20,
numValue3: 30,
numValue4: 40

If I wanted to add a computed field to my pipeline using one of the numeric fields, I could do something like this:

db.myCollection.aggregate(
{ 
  $project : {
    someComputedField : { $add:["$numValue1", 15] }
  }
})

If I wanted to add a computed field based on two of these fields, I know I could do something like this:

db.myCollection.aggregate(
{ 
  $project : {
    someComputedField : { $add:["$numValue1", "$numValue2"] }
  }
})

Now, my question is what if I needed to do $numValue1 + $numValue2 + $numValue3 + $numValue4?

Or even more interestingly $numValue1 * ($numValue2 + $numValue3) / $numValue4?

2 Answers 2

5

$add can accept multiple values, so the first case would be:

someComputedField: {$add: ['$numValue1', '$numValue2', '$numValue3', '$numValue4']}

And you can nest operators, so the second case would be:

someComputedField: {
    $divide: [
        {$multiply: [
            '$numValue1', {$add: ['$numValue2', '$numValue3']}
        ]},
        '$numValue4'
    ]
}
Sign up to request clarification or add additional context in comments.

4 Comments

Adding multiple values to $multiply worked but I'm getting "exception: the $multiply operator does not accept an object as an operand" when I nest operators like you suggested.
@Edenbauer Worked when I tried it with Mongo 2.2.2. What version are you using?
you may missing [ ] and are trying to pass an object instead of an array to it?
Turns out I was using an older version. Thanks @JohnnyHK!!
-1

Better to use map/reduce when query is not simple add.

db.myCollection.mapReduce(function() {
   emit('result', this.numValue1 * (this.numValue2 + this.numValue3) / this.numValue4);
}, function(key, values) {
   var i=0; values.forEach(function(n){i+=n;}); return i;
});

Also aggregation framework and map/reduce have nearly same performance.

3 Comments

100% incorrect that aggregation framework and map/reduce have nearly the same performance. Aggregation framework is significantly faster than map/reduce.
downvoters @Asya , I previously did a test for comparing performance of aggregation framework and map/reduce (with 4million rows, each about 350bytes), and performance was nearly same. I opened a new question in stackoverflow.com/q/13908438/326792 , answer on it, and publish your practice.
your linked question shows a very small dataset used to do grouping by. I don't think you should jump any performance conclusion on the basis on a single test on 200K documents. Btw, I ran your example and got about 5x slower w/MapReduce even with that data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.