0
0
MongoDBquery~20 mins

$avg accumulator in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $avg Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Calculate average score per student
Given the following documents in the 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" } } }
])
A[ { "$project": { "name": 1, "avgScore": { "$avg": "$scores" } } } ]
B[ { "$group": { "_id": "$name", "avgScore": { "$avg": "$scores" } } } ]
C[ { "$project": { "name": 1, "avgScore": { "$avg": "$scores.score" } } } ]
D[ { "$group": { "_id": "$name", "avgScore": { "$avg": "$scores.score" } } } ]
Attempts:
2 left
💡 Hint
Use $project to calculate average on an array field within each document.
query_result
intermediate
2:00remaining
Average price per category
Consider a 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" } } }
])
A[ { "$group": { "_id": "$category", "avgPrice": { "$avg": "$category.price" } } } ]
B[ { "$project": { "category": 1, "avgPrice": { "$avg": "$price" } } } ]
C[ { "$group": { "_id": "$category", "avgPrice": { "$avg": "$price" } } } ]
D[ { "$match": { "price": { "$exists": true } } }, { "$group": { "_id": "$category", "avgPrice": { "$avg": "$price" } } } ]
Attempts:
2 left
💡 Hint
Use $group to aggregate by category and calculate average price.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in $avg usage
Which of the following aggregation stages will cause a syntax error due to incorrect $avg usage?
A{ $project: { average: { $avg: "$values" } } }
B{ $group: { _id: "$type", average: { $avg: "$value" } } }
C{ $group: { _id: "$type", average: { $avg: ["$value1", "$value2"] } } }
D{ $group: { _id: "$type", average: { $avg: "$value", $sum: 1 } } }
Attempts:
2 left
💡 Hint
Check if the accumulator object contains invalid extra fields.
optimization
advanced
2:00remaining
Optimize average calculation on large dataset
You want to calculate the average rating from a large reviews collection. Which pipeline is more efficient?
A[ { $match: { rating: { $exists: true } } }, { $group: { _id: null, avgRating: { $avg: "$rating" } } } ]
B[ { $group: { _id: null, avgRating: { $avg: "$rating" } } } ]
C[ { $project: { rating: 1 } }, { $group: { _id: null, avgRating: { $avg: "$rating" } } } ]
D[ { $group: { _id: "$userId", avgRating: { $avg: "$rating" } } } ]
Attempts:
2 left
💡 Hint
Filtering out documents without rating before grouping can improve performance.
🧠 Conceptual
expert
2:00remaining
Understanding $avg behavior with missing or null values
What is the result of the following aggregation on this 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" } } }])
Anull
B100
C50
D0
Attempts:
2 left
💡 Hint
Consider how $avg treats null or missing fields in aggregation.