What is an aggregation pipeline?
An aggregation pipeline is a set of aggregation stages (operations) executed on documents to produce a result
What is the general syntax for aggregation pipelinies?
db.collection.aggregate([{pipeline1}, {pipeline2}], {options}) returns cursorDo aggregation pipelines modify documents in their respective collections?
No, unless a $merge or $out is used
True or False
When you run aggregation pipelines on MongoDB Atlas deployments in the MongoDB Atlas UI, you can preview the results at each stage
True
What are the 7 most common aggregation stages?
Can the same aggregation stages be used multiple times in the same pipeline?
Yes, except $merge and $out
What is the purpose of field paths in aggregation pipelines?
The purpose of field paths in aggregation pipelines is to provide access to the field values of input documents. Syntax: "$fieldName"
Code an example of the aggregation pipeline structure
db.collection.aggregate([{$stage1: {...}}, {$stage2: {...}}])What is the purpose of $match?
The purpose of $match is to filter documents in an aggregation pipeline
What is the purpose of $group?
The purpose of $group is to separate documents into groups according to a group key. This is normally done to perform accumulator operations
Code an example of a $group stage
db.collection.aggregate([{$group: {_id: "$groupKey", field: {$accumulatorOperator: expression}}}])Code an example using the $sort and $limit stages
db.collection.aggregate([{$sort: {field: 1}}, {$limit: 3}])What is the purpose of the $project stage?
The purpose of the $project stage is to select the fields that should be displayed in output documents. It can also be used to set or add new fields
Code an example of the $count stage
db.collection.aggregate([{$match: ...}, {$count: fieldNameOfOutputDocument}])What is the purpose of the $out stage?
The purpose of the $out stage is to insert the resulting documents of an aggregation pipeline into a new collection. However, if an existing collection is selected, all of the documents in that collection will be replaced
True or False
The $out stage must be the last stage in an aggregation pipeline
True
Code an example using the $project stage
db.collection.aggregate([{$project: {field: 1 or 0}}])What are the 2 main stages that can benefit from indexes?
$match$sortWhat does aggregate() return?
A cursor
True or False
During the pipeline processing, the documents may exceed the 16 MB size
True. However, care must be taken to ensure when the documents are returned to the client, they do not exceed the 16 MB limit
How many aggregation pipeline stages are allowed in an aggregation pipeline?
1000 stages
What is the output of a $group stage?
The output of a $group stage is one document for each group key
Code an example of using a $lookup stage and explain how it works
db.collection.aggregate({$lookup: {from: "otherCollection", localField: "fieldName", foreignField: "otherField", as: "fieldArrayName"}})If the local field value matches the foreign field value, the foreign document will be appended to the field array name
What are the 2 general structures of the $out stage?
1 db.collection.aggregate([{$out: "collectionName"}])
2 db.collection.aggregate([{$out: {db: "databaseName", coll: "collectionName"}}])