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.
38 lines
1.5 KiB
38 lines
1.5 KiB
# Mongo - Aggregation
|
|
|
|
## Lesson Objectives
|
|
1. Explain what aggregation is
|
|
1. Explain the Mongo Aggregation Pipeline
|
|
1. Explain some basic aggregation functions
|
|
1. Explain $out
|
|
|
|
## Explain what aggregation is
|
|
|
|
## Explain the Mongo Aggregation Pipeline
|
|
1. `db.collectionName.aggregate([<stage>, <stage>]);`
|
|
1. http://docs.mongodb.org/manual/_images/aggregation-pipeline.png
|
|
|
|
## Explain some basic aggregation functions
|
|
1. `{ $match : { weight : { $lt : 600 } } }`
|
|
1. `{ $sort : { salary : -1 } }`
|
|
1. `{ $limit : 1 }`
|
|
1. `{ $skip : 1 }`
|
|
1. `{ $out : 'collectionName'}`
|
|
1. $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
|
|
- `{ $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?
|