Skip to main content

Mongo - Aggregation

Lesson Objectives#

  1. Explain what aggregation is
  2. Explain the Mongo Aggregation Pipeline
  3. Explain some basic aggregation functions
  4. Explain $out

Explain what aggregation is#

Explain the Mongo Aggregation Pipeline#

  1. db.collectionName.aggregate([<stage>, <stage>]);
  2. http://docs.mongodb.org/manual/_images/aggregation-pipeline.png

Explain some basic aggregation functions#

  1. { $match : { weight : { $lt : 600 } } }
  2. { $sort : { salary : -1 } }
  3. { $limit : 1 }
  4. { $skip : 1 }
  5. { $out : 'collectionName'}
  6. $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' } } }
  1. { $unwind : '$loves' }
  • db.employees.aggregate([{$unwind : '$loves'}, {$group : { _id : '$loves', num : {$sum : 1}, names : {$addToSet : '$name'}}}])
  1. $project