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
1.5 KiB
Mongo - Aggregation
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
{ $project : { name : 1, _id : 0 } }{ $project : { namezz :'$name' } }- expressions
{ $project : { awesomeName : { $concat : ['$name', ' is awesome', '!']}} }- http://docs.mongodb.org/manual/meta/aggregation-quick-reference/#aggregation-expressions
{ $project : { embedded_atrribute : '$embedded_object.foo' } }{ $project : { embedded_object.attribute : 1 } }- arrays?