0
0
MongoDBquery~20 mins

Aggregation for reporting dashboards in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Aggregation Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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" } } }
])
A[{ $group: { _id: "$category", totalSales: { $max: "$amount" } } }]
B[{ $match: { amount: { $gt: 0 } } }, { $group: { _id: "$category", totalSales: { $avg: "$amount" } } }]
C[{ $group: { _id: "$category", totalSales: { $sum: "$amount" } } }]
D[{ $project: { category: 1, totalSales: { $sum: "$amount" } } }]
Attempts:
2 left
💡 Hint
Use $group to aggregate data by category and $sum to add amounts.
query_result
intermediate
2: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" }
])
A[{ $match: { totalPrice: { $gt: 100 } } }, { $count: "highValueOrders" }]
B[{ $match: { totalPrice: { $gte: 100 } } }, { $count: "highValueOrders" }]
C[{ $group: { _id: null, count: { $sum: 1 } } }, { $match: { totalPrice: { $gt: 100 } } }]
D[{ $project: { highValue: { $gt: ["$totalPrice", 100] } } }, { $count: "highValueOrders" }]
Attempts:
2 left
💡 Hint
Filter first, then count the matching documents.
📝 Syntax
advanced
2: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 } }
])
A[{ $group: { _id: "$city", totalUsers: { $sum: 1 } } }, { $sort: { totalUsers: -1 } }]
B[{ $group: { _id: "$city", totalUsers: { $sum: 1 } } }, { $sort: { totalUsers: "desc" } }]
C[{ $group: { _id: "$city", totalUsers: { $sum: 1 } } }, { $sort: { totalUsers: 1 } }]
D[{ $group: { _id: "$city", totalUsers: { $sum: 1 } } }, { $sort: { totalUsers: 0 } }]
Attempts:
2 left
💡 Hint
Check the value used for sorting order in $sort stage.
optimization
advanced
2: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?
A[{ $project: { productId: 1, rating: 1 } }, { $group: { _id: "$productId", avgRating: { $avg: "$rating" } } }]
B[{ $group: { _id: "$productId", avgRating: { $avg: "$rating" } } }, { $match: { avgRating: { $gte: 4 } } }]
C[{ $group: { _id: "$productId", avgRating: { $avg: "$rating" } } }]
D[{ $match: { rating: { $exists: true } } }, { $group: { _id: "$productId", avgRating: { $avg: "$rating" } } }]
Attempts:
2 left
💡 Hint
Filter out documents missing rating before grouping to reduce workload.
🧠 Conceptual
expert
2:00remaining
Understanding $facet stage for dashboard reports
Which option best describes the purpose of the $facet stage in MongoDB aggregation pipelines for reporting dashboards?
AIt allows running multiple aggregation pipelines in parallel and returns their results in a single document.
BIt filters documents based on multiple conditions combined with AND logic.
CIt sorts documents by multiple fields in a single stage.
DIt groups documents by multiple fields and calculates sums for each.
Attempts:
2 left
💡 Hint
Think about how to get different summaries in one query.