0
0
MongoDBquery~10 mins

$min and $max accumulators in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $min and $max accumulators
Start Aggregation Pipeline
Group Documents by _id
Apply $min and $max on Fields
Calculate Minimum and Maximum Values
Output Grouped Result with Min and Max
End
The aggregation groups documents, then calculates minimum and maximum values for specified fields using $min and $max accumulators.
Execution Sample
MongoDB
db.sales.aggregate([
  { $group: {
      _id: "$item",
      minPrice: { $min: "$price" },
      maxPrice: { $max: "$price" }
    }
  }
])
This query groups sales by item and finds the minimum and maximum price for each item.
Execution Table
StepDocument ProcessedGroup _idCurrent minPriceCurrent maxPriceAction
1{item: "apple", price: 5}apple55Initialize minPrice and maxPrice with 5
2{item: "apple", price: 3}apple35Update minPrice to 3 (3 < 5)
3{item: "apple", price: 8}apple38Update maxPrice to 8 (8 > 5)
4{item: "banana", price: 2}banana22Initialize minPrice and maxPrice with 2
5{item: "banana", price: 4}banana24Update maxPrice to 4 (4 > 2)
6{item: "banana", price: 1}banana14Update minPrice to 1 (1 < 2)
7No more documents---Aggregation completes, output grouped results
💡 All documents processed; final min and max values computed per group.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
minPrice (apple)N/A5333333
maxPrice (apple)N/A5588888
minPrice (banana)N/AN/AN/AN/A2211
maxPrice (banana)N/AN/AN/AN/A2444
Key Moments - 3 Insights
Why does minPrice update when a smaller price is found but not when a larger price is found?
Because $min only updates when the new value is less than the current minPrice, as shown in execution_table rows 2 and 3.
Why do we initialize minPrice and maxPrice with the first document's price?
Initialization sets a starting point for comparison; without it, we wouldn't have a baseline to compare other prices, as seen in rows 1 and 4.
What happens if all documents for a group have the same price?
Both minPrice and maxPrice remain that same value since no smaller or larger values appear, consistent with the logic in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is maxPrice for 'apple' after processing the third document?
A3
B8
C5
DUndefined
💡 Hint
Check row 3 under 'Current maxPrice' for 'apple'.
At which step does minPrice for 'banana' update to 1?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look at the 'Current minPrice' for 'banana' in rows 4, 5, and 6.
If the price of the second 'apple' document was 6 instead of 3, what would minPrice be after step 2?
A5
B3
C6
DUndefined
💡 Hint
Compare the new price with the initial minPrice in row 1 and see if it updates in row 2.
Concept Snapshot
$min and $max accumulators find the smallest and largest values in grouped documents.
Use inside $group stage in aggregation pipeline.
Syntax: { $min: "$field" }, { $max: "$field" }.
They update values only when a smaller or larger value appears.
Useful for summarizing data ranges per group.
Full Transcript
This visual execution shows how MongoDB's $min and $max accumulators work in an aggregation pipeline. Documents are grouped by the 'item' field. For each group, the pipeline tracks the minimum and maximum 'price' values. Initially, the first document's price sets both minPrice and maxPrice. As more documents are processed, minPrice updates only if a smaller price is found, and maxPrice updates only if a larger price is found. The execution table traces each document's effect on these values step-by-step. The variable tracker summarizes how minPrice and maxPrice change for each group. Key moments clarify common confusions, such as why initialization is needed and how updates happen. The quiz tests understanding by referencing specific steps and values in the execution. This helps beginners see exactly how $min and $max accumulate values during aggregation.