Challenge - 5 Problems
MongoDB $avg Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Calculate average score per student
Given the following documents in the
Which aggregation pipeline will correctly calculate the average score for each student?
students collection:{ "name": "Alice", "scores": [80, 90, 100] }
{ "name": "Bob", "scores": [70, 85, 90] }
{ "name": "Charlie", "scores": [60, 75, 85] }Which aggregation pipeline will correctly calculate the average score for each student?
MongoDB
db.students.aggregate([
{ $project: { name: 1, avgScore: { $avg: "$scores" } } }
])Attempts:
2 left
💡 Hint
Use $project to calculate average on an array field within each document.
✗ Incorrect
The $avg accumulator can calculate the average of values in an array when used inside $project. Using $group here is incorrect because scores is an array inside each document, not a field to group by.
❓ query_result
intermediate2:00remaining
Average price per category
Consider a
Which aggregation pipeline will return the average price for each category?
products collection with documents like:{ "category": "Books", "price": 15 }
{ "category": "Books", "price": 25 }
{ "category": "Electronics", "price": 100 }Which aggregation pipeline will return the average price for each category?
MongoDB
db.products.aggregate([
{ $group: { _id: "$category", avgPrice: { $avg: "$price" } } }
])Attempts:
2 left
💡 Hint
Use $group to aggregate by category and calculate average price.
✗ Incorrect
The $group stage groups documents by category and calculates the average price. $project cannot aggregate across documents. Option C adds an unnecessary $match but does not change correctness.
📝 Syntax
advanced2:00remaining
Identify the syntax error in $avg usage
Which of the following aggregation stages will cause a syntax error due to incorrect $avg usage?
Attempts:
2 left
💡 Hint
Check if the accumulator object contains invalid extra fields.
✗ Incorrect
Option D incorrectly mixes $avg and $sum inside the same object, which is invalid syntax. Each accumulator must be a single operator.
❓ optimization
advanced2:00remaining
Optimize average calculation on large dataset
You want to calculate the average rating from a large
reviews collection. Which pipeline is more efficient?Attempts:
2 left
💡 Hint
Filtering out documents without rating before grouping can improve performance.
✗ Incorrect
Option A filters documents to only those with a rating before grouping, reducing data processed in $group. Option A processes all documents, including those without rating. Option A projects but does not filter. Option A groups by userId, not overall average.
🧠 Conceptual
expert2:00remaining
Understanding $avg behavior with missing or null values
What is the result of the following aggregation on this
Documents:
Pipeline:
What will
sales collection?Documents:
{ "amount": 100 }
{ "amount": null }
{ }Pipeline:
[ { $group: { _id: null, averageAmount: { $avg: "$amount" } } } ]What will
averageAmount be?MongoDB
db.sales.aggregate([{ $group: { _id: null, averageAmount: { $avg: "$amount" } } }])Attempts:
2 left
💡 Hint
Consider how $avg treats null or missing fields in aggregation.
✗ Incorrect
$avg ignores null and missing values when calculating the average. Only the value 100 is considered, so the average is 100.