0
0
MongoDBquery~20 mins

$min and $max accumulators in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB MinMax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find the minimum price in the products collection

Given a products collection with documents containing a price field, which aggregation query returns the minimum price?

MongoDB
db.products.aggregate([
  { $group: { _id: null, minPrice: { $min: "$price" } } }
])
A[{ "_id": null, "minPrice": 100 }]
B[{ "_id": null, "maxPrice": 100 }]
C[{ "_id": null, "minPrice": 5 }]
D[{ "_id": null, "minPrice": null }]
Attempts:
2 left
💡 Hint

Use $group stage with $min accumulator on the price field.

query_result
intermediate
2:00remaining
Find the maximum score per player

Given a scores collection with documents containing player and score fields, which aggregation query returns the highest score for each player?

MongoDB
db.scores.aggregate([
  { $group: { _id: "$player", maxScore: { $max: "$score" } } }
])
A[{ "_id": "Alice", "maxScore": 95 }, { "_id": "Bob", "maxScore": 88 }]
B[{ "_id": "Alice", "maxScore": 88 }, { "_id": "Bob", "maxScore": 95 }]
C[{ "_id": null, "maxScore": 95 }]
D[{ "_id": "Alice", "minScore": 88 }, { "_id": "Bob", "minScore": 95 }]
Attempts:
2 left
💡 Hint

Group by player and use $max on score.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this aggregation using $min

Which option contains a syntax error in the aggregation pipeline that uses $min?

MongoDB
db.orders.aggregate([
  { $group: { _id: "$customer", minOrder: { $min: "amount" } } }
])
ASyntaxError: Missing comma after _id field
BNo error, runs correctly
CTypeError: $min requires numeric values only
DSyntaxError: Field path must be prefixed with '$' inside $min accumulator
Attempts:
2 left
💡 Hint

Check if the field name inside $min is correctly referenced.

optimization
advanced
2:00remaining
Optimize aggregation to find min and max in one pass

You want to find both the minimum and maximum rating in the reviews collection. Which aggregation pipeline is the most efficient?

Adb.reviews.aggregate([{ $group: { _id: null, minRating: { $min: "$rating" } } }, { $group: { _id: null, maxRating: { $max: "$rating" } } }])
Bdb.reviews.aggregate([{ $group: { _id: null, minRating: { $min: "$rating" }, maxRating: { $max: "$rating" } } }])
C)]} } } "gnitar$" :xam$ { :gnitaRxam ,llun :di_ { :puorg$ { ,} } } "gnitar$" :nim$ { :gnitaRnim ,llun :di_ { :puorg$ {[(etagergga.sweiver.bd
Ddb.reviews.aggregate([{ $group: { _id: null, minRating: { $min: "$rating" } } }, { $project: { maxRating: { $max: "$rating" } } }])
Attempts:
2 left
💡 Hint

Try to compute both min and max in a single $group stage.

🧠 Conceptual
expert
3:00remaining
Behavior of $min and $max with missing fields

Consider documents in a collection where some documents lack the score field. What is the behavior of $min and $max accumulators when grouping on score?

A<p>Documents missing <code>score</code> are ignored in <code>$min</code> and <code>$max</code> calculations.</p>
B<p>Documents missing <code>score</code> cause <code>$min</code> and <code>$max</code> to return null.</p>
C<p>Documents missing <code>score</code> are treated as zero in <code>$min</code> and <code>$max</code>.</p>
D<p>Documents missing <code>score</code> cause the aggregation to fail with an error.</p>
Attempts:
2 left
💡 Hint

Think about how MongoDB handles missing fields in accumulators.