Given a products collection with documents containing a price field, which aggregation query returns the minimum price?
db.products.aggregate([
{ $group: { _id: null, minPrice: { $min: "$price" } } }
])Use $group stage with $min accumulator on the price field.
The $min accumulator finds the smallest value of the specified field across all documents grouped. Here, grouping by null aggregates all documents together.
Given a scores collection with documents containing player and score fields, which aggregation query returns the highest score for each player?
db.scores.aggregate([
{ $group: { _id: "$player", maxScore: { $max: "$score" } } }
])Group by player and use $max on score.
The $max accumulator returns the highest value per group. Grouping by player gives max score per player.
Which option contains a syntax error in the aggregation pipeline that uses $min?
db.orders.aggregate([
{ $group: { _id: "$customer", minOrder: { $min: "amount" } } }
])Check if the field name inside $min is correctly referenced.
Field names inside accumulators must be prefixed with $. Here, "amount" should be "$amount".
You want to find both the minimum and maximum rating in the reviews collection. Which aggregation pipeline is the most efficient?
Try to compute both min and max in a single $group stage.
Using one $group stage with both $min and $max is more efficient than multiple stages.
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?
Think about how MongoDB handles missing fields in accumulators.
MongoDB ignores documents where the field is missing or null when calculating $min and $max. They do not cause errors or treat missing as zero.