Aggregation Native Flashcards
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 cursor
Do 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?
- $match
- $group
- $sort
- $set
- $project
- $out
- $merge
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