Bird
Raised Fist0
MongoDBquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What do the $min and $max accumulators do in a MongoDB aggregation pipeline?
easy
A. $min and $max sort documents in ascending and descending order respectively.
B. $min and $max both find the average value in a group.
C. $min finds the first document and $max finds the last document in a group.
D. $min finds the smallest value and $max finds the largest value in a group.

Solution

  1. Step 1: Understand the purpose of $min and $max

    $min returns the smallest value from the grouped documents, while $max returns the largest value.
  2. Step 2: Compare with other options

    The other options describe different operations like average, first/last document, or sorting, which are not what $min and $max do.
  3. Final Answer:

    $min finds the smallest value and $max finds the largest value in a group. -> Option D
  4. Quick Check:

    $min = smallest, $max = largest [OK]
Hint: Remember: min = smallest, max = largest value [OK]
Common Mistakes:
  • Confusing $min and $max with average functions
  • Thinking they return documents instead of values
  • Mixing accumulators with sorting operators
2. Which of the following is the correct syntax to use $max in a $group stage to find the highest score from a field named score?
easy
A. { $group: { _id: null, maxScore: { $max: "$score" } } }
B. { $group: { _id: null, maxScore: { max: "$score" } } }
C. { $group: { _id: null, maxScore: { $max: score } } }
D. { $group: { _id: null, maxScore: { $max: "score" } } }

Solution

  1. Step 1: Check the correct use of $max accumulator

    The correct syntax requires $max as the operator and the field name as a string with a $ prefix inside quotes: "$score".
  2. Step 2: Identify syntax errors in other options

    { $group: { _id: null, maxScore: { max: "$score" } } } misses the $ before max, { $group: { _id: null, maxScore: { $max: score } } } lacks quotes around field name, and { $group: { _id: null, maxScore: { $max: "score" } } } uses quotes without $ prefix, all invalid.
  3. Final Answer:

    { $group: { _id: null, maxScore: { $max: "$score" } } } -> Option A
  4. Quick Check:

    Use $max: "$field" syntax [OK]
Hint: Always prefix field names with $ and use quotes inside accumulators [OK]
Common Mistakes:
  • Omitting $ before accumulator name
  • Not quoting the field name with $
  • Using field name without $ or quotes
3. Given the collection documents:
{ "name": "Alice", "score": 85 }, { "name": "Bob", "score": 92 }, { "name": "Charlie", "score": 78 }

What will be the output of this aggregation?
[{ $group: { _id: null, minScore: { $min: "$score" }, maxScore: { $max: "$score" } } }]
medium
A. { "_id": null, "minScore": 85, "maxScore": 85 }
B. { "_id": null, "minScore": 78, "maxScore": 92 }
C. { "_id": null, "minScore": 78, "maxScore": 78 }
D. { "_id": null, "minScore": 92, "maxScore": 85 }

Solution

  1. Step 1: Identify minimum and maximum scores from documents

    The scores are 85, 92, and 78. The smallest is 78 and the largest is 92.
  2. Step 2: Understand the aggregation output

    The $group stage groups all documents (_id: null) and calculates minScore as 78 and maxScore as 92.
  3. Final Answer:

    { "_id": null, "minScore": 78, "maxScore": 92 } -> Option B
  4. Quick Check:

    Min = 78, Max = 92 [OK]
Hint: Scan values, pick smallest for $min and largest for $max [OK]
Common Mistakes:
  • Mixing up min and max values
  • Assuming $min or $max returns documents instead of values
  • Forgetting to use $ before field names
4. Consider this aggregation pipeline:
[{ $group: { _id: "$category", minPrice: { $min: price }, maxPrice: { $max: "$price" } } }]

What is the error in this pipeline?
medium
A. Field name 'price' in $min is missing quotes and $ prefix.
B. Using $max and $min together is not allowed.
C. The _id field cannot be a field name.
D. The pipeline is correct and has no errors.

Solution

  1. Step 1: Check syntax of $min and $max usage

    The $min accumulator uses price without quotes and $ prefix, which is incorrect. It should be "$price".
  2. Step 2: Verify other parts of the pipeline

    $max is correctly used with "$price". Grouping by "$category" is valid. Using both accumulators together is allowed.
  3. Final Answer:

    Field name 'price' in $min is missing quotes and $ prefix. -> Option A
  4. Quick Check:

    Field names need "$" prefix inside accumulators [OK]
Hint: Always quote and prefix field names with $ inside accumulators [OK]
Common Mistakes:
  • Omitting $ before field names inside accumulators
  • Thinking $min and $max cannot be used together
  • Misunderstanding _id field usage in $group
5. You have a collection of sales documents with fields region and amount. You want to find the smallest and largest sale amounts per region, but only for regions where the smallest sale is above 100. Which aggregation pipeline correctly achieves this?
hard
A. [{ $group: { _id: "$region", minSale: { $min: "$amount" }, maxSale: { $max: "$amount" }, filter: { $gt: ["$minSale", 100] } } }]
B. [{ $match: { amount: { $gt: 100 } } }, { $group: { _id: "$region", minSale: { $min: "$amount" }, maxSale: { $max: "$amount" } } }]
C. [{ $group: { _id: "$region", minSale: { $min: "$amount" }, maxSale: { $max: "$amount" } } }, { $match: { minSale: { $gt: 100 } } }]
D. [{ $group: { _id: "$region", minSale: { $min: "$amount" }, maxSale: { $max: "$amount" } } }, { $match: { maxSale: { $gt: 100 } } }]

Solution

  1. Step 1: Group by region and calculate min and max sales

    The $group stage groups documents by region and calculates minSale and maxSale using $min and $max on amount.
  2. Step 2: Filter groups where minSale is greater than 100

    The $match stage after grouping filters groups where minSale is greater than 100, exactly as required.
  3. Final Answer:

    [{ $group: { _id: "$region", minSale: { $min: "$amount" }, maxSale: { $max: "$amount" } } }, { $match: { minSale: { $gt: 100 } } }] -> Option C
  4. Quick Check:

    Group first, then filter on aggregated minSale [OK]
Hint: Filter after grouping to use aggregated fields in conditions [OK]
Common Mistakes:
  • Filtering before grouping, which filters documents not groups
  • Trying to filter inside $group stage incorrectly
  • Filtering on maxSale instead of minSale
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard