0
0
MongoDBquery~20 mins

$facet for multiple pipelines in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Facet Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of $facet with two pipelines
Given the following MongoDB aggregation pipeline using $facet, what is the output?
MongoDB
db.sales.aggregate([
  {
    $facet: {
      totalSales: [
        { $group: { _id: null, total: { $sum: "$amount" } } }
      ],
      averageSales: [
        { $group: { _id: null, avg: { $avg: "$amount" } } }
      ]
    }
  }
])
A[{ "totalSales": 500, "averageSales": 50 }]
B[{ "totalSales": [{ "_id": null, "total": 500 }], "averageSales": [{ "_id": null, "avg": 50 }] }]
C[{ "totalSales": [{ "total": 500 }], "averageSales": [{ "avg": 50 }] }]
D[{ "totalSales": [{ "_id": null, "total": 50 }], "averageSales": [{ "_id": null, "avg": 500 }] }]
Attempts:
2 left
💡 Hint
Remember that each facet pipeline returns an array of documents.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in $facet usage
Which option contains a syntax error in the use of $facet in MongoDB aggregation?
MongoDB
db.collection.aggregate([
  {
    $facet: {
      pipeline1: [ { $match: { status: "A" } } ],
      pipeline2: { $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
    }
  }
])
AUsing $group inside $facet is not allowed
Bpipeline1 uses $match which is invalid inside $facet
CMissing comma between pipeline1 and pipeline2
Dpipeline2 is not an array of stages, it is a single stage object
Attempts:
2 left
💡 Hint
Each pipeline inside $facet must be an array of stages.
optimization
advanced
2:00remaining
Optimizing $facet pipelines for performance
You have two pipelines inside a $facet stage: one counts documents with status 'A', the other calculates average amount for status 'B'. Which option optimizes performance best?
AUse $match for status 'A' and 'B' inside each pipeline separately
BUse $project to filter status inside each pipeline
CUse a single $match stage before $facet filtering status 'A' or 'B'
DUse $group first then $match inside each pipeline
Attempts:
2 left
💡 Hint
Filtering early reduces data processed in later stages.
🔧 Debug
advanced
2:00remaining
Why does this $facet pipeline return empty arrays?
Given this aggregation, why do both facets return empty arrays?
db.orders.aggregate([
  { $facet: {
      shipped: [ { $match: { status: "shipped" } }, { $count: "count" } ],
      pending: [ { $match: { status: "pending" } }, { $count: "count" } ]
    }
  }
])
ANo documents have status 'shipped' or 'pending' in the collection
BThe $count stage is misplaced and should be outside $facet
CThe $match stages are invalid inside $facet
DThe aggregation pipeline is missing a $group stage before $count
Attempts:
2 left
💡 Hint
Empty arrays mean no documents matched the filters.
🧠 Conceptual
expert
2:00remaining
Understanding $facet output structure
What is the structure of the output document from a MongoDB aggregation pipeline using $facet with three pipelines named 'A', 'B', and 'C'?
AA single document with keys 'A', 'B', 'C', each containing an array of documents from their respective pipelines
BAn array of documents, each containing keys 'A', 'B', 'C' with single documents inside
CMultiple documents, one per pipeline, each with a key named after the pipeline
DA flat array combining all documents from pipelines 'A', 'B', and 'C'
Attempts:
2 left
💡 Hint
Think about how $facet groups results from multiple pipelines.