What does the $facet stage do in a MongoDB aggregation pipeline?
The $facet stage allows running multiple aggregation pipelines within a single stage. It outputs a document where each field contains the results of one pipeline, letting you perform different analyses on the same input data simultaneously.
Click to reveal answer
beginner
How does $facet help when you want to get multiple summaries from the same collection?
$facet runs multiple pipelines in parallel on the same input documents. This means you can get different summaries, like counts, averages, or filtered lists, all in one query without running multiple separate queries.
Click to reveal answer
intermediate
Can you explain the output format of a $facet stage?
The output of $facet is a single document with fields named after each pipeline. Each field contains an array of documents that are the results of that pipeline. For example, { counts: [...], averages: [...] }.
Click to reveal answer
intermediate
Why might you use $facet instead of running multiple aggregation queries?
Using $facet reduces the number of queries to the database, improving performance and consistency. It also simplifies code by combining multiple analyses into one pipeline.
Click to reveal answer
beginner
Give an example use case for $facet in a real-world scenario.
Imagine an online store wanting to see total sales, average order value, and a list of top products all at once. $facet can run these three pipelines in parallel on the orders collection and return all results in one response.
Click to reveal answer
What is the main purpose of the $facet stage in MongoDB?
AFilter documents based on a condition
BRun multiple aggregation pipelines in parallel on the same input
CSort documents by a field
DJoin two collections
✗ Incorrect
$facet runs multiple pipelines simultaneously on the same input documents.
What type of output does $facet produce?
AA document with fields containing arrays of results from each pipeline
BA single array of documents
CA count of documents
DA sorted list of documents
✗ Incorrect
$facet outputs a document where each field holds an array of results from its pipeline.
Which of these is NOT a benefit of using $facet?
AAutomatically indexing fields
BReducing the number of database queries
CGetting consistent results from the same data snapshot
DCombining multiple analyses in one query
✗ Incorrect
$facet does not create indexes; it runs multiple pipelines in one aggregation.
If you want to get both total count and average value from a collection in one query, which stage helps?
A<code>$match</code>
B<code>$group</code>
C<code>$facet</code>
D<code>$sort</code>
✗ Incorrect
$facet can run separate pipelines for count and average simultaneously.
Inside a $facet stage, how do you define each pipeline?
AAs a single aggregation stage
BAs a query filter
CAs a JavaScript function
DAs an array of aggregation stages under a named field
✗ Incorrect
Each pipeline is an array of stages assigned to a field name inside $facet.
Describe how the $facet stage works and why it is useful in MongoDB aggregation.
Think about running many analyses on the same data at once.
You got /4 concepts.
Explain the structure of the output document from a $facet stage with two pipelines named 'counts' and 'averages'.
Imagine the output as a box with two labeled compartments holding lists.
You got /3 concepts.
Practice
(1/5)
1. What does the $facet stage do in a MongoDB aggregation pipeline?
easy
A. Sorts documents in ascending or descending order.
B. Filters documents based on a condition.
C. Joins two collections together.
D. Runs multiple pipelines on the same input data and returns results in one document.
Solution
Step 1: Understand the purpose of $facet
$facet allows running multiple aggregation pipelines on the same input data simultaneously.
Step 2: Identify the output structure
The output is a single document containing separate arrays for each pipeline's results.
Final Answer:
Runs multiple pipelines on the same input data and returns results in one document. -> Option D
Quick Check:
$facet = multiple pipelines, one output document [OK]
Hint: Remember: $facet runs many pipelines at once [OK]
Common Mistakes:
Thinking $facet filters or sorts data
Confusing $facet with $lookup
Assuming it returns multiple documents instead of one
2. Which of the following is the correct syntax to use $facet in a MongoDB aggregation pipeline?
$facet expects an object where each key is a pipeline name and the value is an array of pipeline stages.
Step 2: Check each option
{ $facet: { pipeline1: [ { $match: {} } ], pipeline2: [ { $group: {} } ] } } correctly uses an object with arrays of stages. Options A, C, and D misuse arrays or objects for stages.
A. The $facet stage must be the last stage in the pipeline.
B. You cannot use $group inside $facet.
C. Pipeline stages inside $facet must be arrays, not objects.
D. The field names inside $group are incorrect.
Solution
Step 1: Check $facet syntax requirements
Each pipeline inside $facet must be an array of stages, not a single object.
Step 2: Identify the error in the given code
The pipeline 'sales' is given as an object with $group, missing array brackets.
Final Answer:
Pipeline stages inside $facet must be arrays, not objects. -> Option C
Quick Check:
$facet pipelines = arrays of stages [OK]
Hint: Always wrap pipeline stages in arrays inside $facet [OK]
Common Mistakes:
Using object instead of array for pipeline stages
Assuming $group is disallowed inside $facet
Thinking $facet must be last stage
5. You want to get both the average price and a list of unique items from a products collection in one query. Which $facet pipeline correctly achieves this?
Each facet must be a named pipeline with an array of stages.
Step 2: Check each option
{ $facet: { avgPrice: [ { $group: { _id: null, avg: { $avg: "$price" } } } ], uniqueItems: [ { $group: { _id: "$item" } } ] } } correctly uses two pipelines: one groups to get average price, the other groups by item to get unique items. Options B, C, and D misuse arrays or operators incorrectly.