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?