Aggregation Native Flashcards

1
Q

What is an aggregation pipeline?

A

An aggregation pipeline is a set of aggregation stages (operations) executed on documents to produce a result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the general syntax for aggregation pipelinies?

A
db.collection.aggregate([{pipeline1}, {pipeline2}], {options}) returns cursor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Do aggregation pipelines modify documents in their respective collections?

A

No, unless a $merge or $out is used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 7 most common aggregation stages?

A
  1. $match
  2. $group
  3. $sort
  4. $set
  5. $project
  6. $out
  7. $merge
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can the same aggregation stages be used multiple times in the same pipeline?

A

Yes, except $merge and $out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of field paths in aggregation pipelines?

A

The purpose of field paths in aggregation pipelines is to provide access to the field values of input documents. Syntax: "$fieldName"

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Code an example of the aggregation pipeline structure

A
db.collection.aggregate([{$stage1: {...}}, {$stage2: {...}}])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of $match?

A

The purpose of $match is to filter documents in an aggregation pipeline

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of $group?

A

The purpose of $group is to separate documents into groups according to a group key. This is normally done to perform accumulator operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Code an example of a $group stage

A
db.collection.aggregate([{$group: {_id: "$groupKey", field: {$accumulatorOperator: expression}}}])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Code an example using the $sort and $limit stages

A
db.collection.aggregate([{$sort: {field: 1}}, {$limit: 3}])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the $project stage?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Code an example of the $count stage

A
db.collection.aggregate([{$match: ...}, {$count: fieldNameOfOutputDocument}])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the purpose of the $out stage?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

True or False

The $out stage must be the last stage in an aggregation pipeline

A

True

17
Q

Code an example using the $project stage

A
db.collection.aggregate([{$project: {field: 1 or 0}}])
18
Q

What are the 2 main stages that can benefit from indexes?

A
  1. $match
  2. $sort
19
Q

What does aggregate() return?

A

A cursor

20
Q

True or False

During the pipeline processing, the documents may exceed the 16 MB size

A

True. However, care must be taken to ensure when the documents are returned to the client, they do not exceed the 16 MB limit

21
Q

How many aggregation pipeline stages are allowed in an aggregation pipeline?

A

1000 stages

22
Q

What is the output of a $group stage?

A

The output of a $group stage is one document for each group key

23
Q

Code an example of using a $lookup stage and explain how it works

A
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

24
Q

What are the 2 general structures of the $out stage?

A
1 db.collection.aggregate([{$out: "collectionName"}])

2 db.collection.aggregate([{$out: {db: "databaseName", coll: "collectionName"}}])
25
Q

True or False

The $out stage does not change any indexes that existed on the previous collection

A

True

26
Q

True or False

If the $out operation modifies a collection with an
Atlas Search index, you must delete and re-create the search index

A

True

27
Q

Which 3 stages cannot appear multiple times in an aggregation pipeline?

A
  1. $out
  2. $merge
  3. $geoNear
28
Q

True or False

An aggregation pipeline can use indexes from the input collection to improve performance.

A

True