0
0
MongoDBquery~5 mins

$min and $max accumulators in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the $min accumulator do in MongoDB aggregation?
The $min accumulator finds the smallest value for a specified field across grouped documents.
Click to reveal answer
beginner
What is the purpose of the $max accumulator in MongoDB?
The $max accumulator returns the largest value for a specified field from grouped documents.
Click to reveal answer
intermediate
How do $min and $max behave when used in a $group stage?
They compute the minimum and maximum values respectively for each group of documents defined by the _id field in the $group stage.
Click to reveal answer
intermediate
Can $min and $max be used on fields with different data types?
They work best on comparable data types like numbers or dates. Mixing incompatible types can lead to unexpected results or errors.
Click to reveal answer
beginner
Example: How to find the minimum and maximum price of products in each category using MongoDB aggregation?
Use $group with _id as category, and $min: "$price" and $max: "$price" to get min and max prices per category.
Click to reveal answer
What does the $min accumulator return in a MongoDB aggregation?
AThe smallest value of a field in grouped documents
BThe largest value of a field in grouped documents
CThe average value of a field
DThe total count of documents
Which accumulator would you use to find the highest score in a group of documents?
A<code>$max</code>
B<code>$min</code>
C<code>$avg</code>
D<code>$sum</code>
In which aggregation stage are $min and $max typically used?
A<code>$sort</code>
B<code>$project</code>
C<code>$match</code>
D<code>$group</code>
What happens if you use $min on a field with mixed data types?
AIt always returns the smallest number
BIt may return unexpected results or errors
CIt converts all values to strings
DIt ignores non-numeric values
How do you specify the field to find the minimum value of in $min?
AUse the field name in an array
BUse the field name as a string without $
CUse the field name prefixed with $ like "$fieldName"
DUse the field name in a nested object
Explain how $min and $max accumulators work in MongoDB aggregation.
Think about grouping documents and finding extreme values.
You got /4 concepts.
    Describe a real-life example where you would use $min and $max in a database query.
    Consider a store with products or a class with student scores.
    You got /4 concepts.