Just for an update. This is possible in aggregation framework in the upcoming release of mongoDB 3.2. They have added a new feature to get the unwind index of array in $unwind stage of mongoDB aggregation called includeArrayIndex.
Using this feature you can use aggregate query of the form:
db.collection.aggregate(
{$unwind: { path: "$vals", includeArrayIndex: "arrayIndex" }},
{$group: { '_id': '$arrayIndex', sum : {$sum: '$vals'}}},
{$sort: { '_id': 1 }},
{$group : { '_id': null, valSum: {$push: '$sum'}}}
)
Explanation:
- The unwind operation unwinds the vals array and also projects the array index of unwind. So the idea is to group similar indexes so that we can sum them
- We then group based on the arrayIndex while summing the vals element. In this stage we have perfectly summed the corresponding index elements
- Next we sort based on arrayIndex so as to prepare for final result array
- Next we group all documents and push the sums to an array called valSum
Output:
{ '_id': null, valSum: [3, 5, 7, 9] }
The main advantage of this approach is that, you need not have arrays of same length. Also it can take any number of input documents. Thanks to the includeArrayIndex feature.
Cheers!
valsarray of same size always ?ith element with theith element and no other pairs.