MongoDB MongoDB Aggregation Framework 2 — Questions and Answers
Question 1: Which aggregation stage performs a left outer join to another collection?
- $join
- $merge
- $lookup (Correct answer)
- $connect
Correct answer: $lookup
$lookup performs a left outer join with another collection in the same database, adding matched documents as an array field.
Question 2: What does the $avg accumulator compute in a $group stage?
- The maximum value
- The minimum value
- The arithmetic mean of numeric values (Correct answer)
- The median value
Correct answer: The arithmetic mean of numeric values
$avg calculates the average of all numeric values for documents in each group.
Question 3: Which stage sorts the documents in the aggregation pipeline?
- $order
- $rank
- $sort (Correct answer)
- $arrange
Correct answer: $sort
$sort reorders pipeline documents according to specified fields, using 1 for ascending and -1 for descending.
Question 4: What is the $addFields stage used for in aggregation?
- Adds new fields to documents without removing existing ones (Correct answer)
- Adds a new collection field
- Inserts fields from another collection
- Adds computed index fields
Correct answer: Adds new fields to documents without removing existing ones
$addFields outputs documents containing all existing fields plus any newly added or modified fields.
Question 5: Which aggregation stage writes the results of the pipeline to a specified collection?
- $export
- $save
- $out (Correct answer)
- $write
Correct answer: $out
$out writes the resulting documents to a specified collection, replacing it if it already exists.
Question 6: What does the $count stage return in an aggregation pipeline?
- An array of all document IDs
- A single document with the count of documents passed to it (Correct answer)
- The count per group
- A running total of documents
Correct answer: A single document with the count of documents passed to it
$count outputs a single document containing a field with the count of documents that entered the stage.
Which aggregation stage performs a left outer join to another collection?