You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1.5 KiB

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' } } }
  7. { $unwind : '$loves' }
    • db.employees.aggregate([{$unwind : '$loves'}, {$group : { _id : '$loves', num : {$sum : 1}, names : {$addToSet : '$name'}}}])
  8. $project