Wednesday 2 April 2014

find the average in nested collection in mongoDB using nodejs

db.collectionName.aggregate([{$match: {"whereColumn": whereCondition}}, {$unwind: '$NestedcolumnToWorkOn}, {$group: {_id: null, "avg": {$avg: "$NestedcolumnToWorkOn.fieldName"}}}], function(err, average) {
console.log(average[0].avg);
});

No comments:

Post a Comment