Challenge - 5 Problems
Aggregation Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Calculate total sales per product category
Given a MongoDB collection
sales with documents containing category and amount fields, which aggregation pipeline produces the total sales amount for each category?MongoDB
db.sales.aggregate([
{ $group: { _id: "$category", totalSales: { $sum: "$amount" } } }
])Attempts:
2 left
💡 Hint
Use $group to aggregate data by category and $sum to add amounts.
✗ Incorrect
Option C groups documents by category and sums the amount field, giving total sales per category. Option C calculates average, not total. Option C finds max amount per category. Option C tries to sum in $project, which is invalid.
❓ query_result
intermediate2:00remaining
Filter and count orders above threshold
Which aggregation pipeline returns the count of orders with
totalPrice greater than 100 in the orders collection?MongoDB
db.orders.aggregate([
{ $match: { totalPrice: { $gt: 100 } } },
{ $count: "highValueOrders" }
])Attempts:
2 left
💡 Hint
Filter first, then count the matching documents.
✗ Incorrect
Option A correctly filters orders with totalPrice > 100 and counts them. Option A groups before filtering, which is incorrect. Option A uses >= 100, not > 100. Option A projects a boolean but does not filter before counting.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this aggregation pipeline
Which option contains a syntax error that will cause the MongoDB aggregation to fail?
MongoDB
db.users.aggregate([
{ $group: { _id: "$city", totalUsers: { $sum: 1 } } },
{ $sort: { totalUsers: -1 } }
])Attempts:
2 left
💡 Hint
Check the value used for sorting order in $sort stage.
✗ Incorrect
Option B uses "desc" as a string for sorting order, which is invalid. MongoDB requires 1 for ascending or -1 for descending. Options A, B, and C use numeric values (though 0 in B is ignored but valid syntax).
❓ optimization
advanced2:00remaining
Optimize aggregation pipeline for large datasets
You want to calculate the average rating per product from a large
reviews collection. Which pipeline is the most efficient?Attempts:
2 left
💡 Hint
Filter out documents missing rating before grouping to reduce workload.
✗ Incorrect
Option D filters documents missing rating before grouping, reducing data processed. Option D groups all documents including those without rating, processing more data than necessary. Option D projects fields but does not filter. Option D filters after grouping, which is less efficient.
🧠 Conceptual
expert2:00remaining
Understanding $facet stage for dashboard reports
Which option best describes the purpose of the
$facet stage in MongoDB aggregation pipelines for reporting dashboards?Attempts:
2 left
💡 Hint
Think about how to get different summaries in one query.
✗ Incorrect
The $facet stage lets you run several pipelines at once and get all results together, useful for dashboards needing multiple reports. Other options describe different stages or operations.