Lesson Objectives#
- Explain what aggregation is
- Explain the Mongo Aggregation Pipeline
- Explain some basic aggregation functions
- Explain $out
Explain what aggregation is#
Explain the Mongo Aggregation Pipeline#
db.collectionName.aggregate([<stage>, <stage>]);
- http://docs.mongodb.org/manual/_images/aggregation-pipeline.png
Explain some basic aggregation functions#
{ $match : { weight : { $lt : 600 } } }
{ $sort : { salary : -1 } }
{ $limit : 1 }
{ $skip : 1 }
{ $out : 'collectionName'}
- $group
{ $group : { _id : '$gender' } }
{ $group : { _id : { 'weight' : '$weight', 'gender' : '$gender' } } } ] );
{ $group : { _id : '$gender', numOfEachGender : { $sum : 1 } } }
{ $group : { _id : '$gender', avgSalary : { $avg : '$salary' } } }
{ $group : { _id : '$weight', namesArray : { $addToSet : '$name' } } }
{ $unwind : '$loves' }
db.employees.aggregate([{$unwind : '$loves'}, {$group : { _id : '$loves', num : {$sum : 1}, names : {$addToSet : '$name'}}}])
- $project