0
0
MongoDBquery~5 mins

$avg accumulator in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the $avg accumulator do in MongoDB aggregation?
It calculates the average (mean) value of numeric values for a group of documents.
Click to reveal answer
beginner
How do you use $avg to find the average score from a field named score in a collection?
Use { $group: { _id: null, averageScore: { $avg: "$score" } } } in an aggregation pipeline.
Click to reveal answer
intermediate
Can $avg be used on non-numeric fields?
No, $avg only works on numeric values. Non-numeric values are ignored.
Click to reveal answer
intermediate
What happens if $avg is used on an empty group of documents?
It returns null because there are no values to average.
Click to reveal answer
beginner
How is $avg different from $sum in MongoDB aggregation?
$avg calculates the mean value, while $sum adds all values together.
Click to reveal answer
What does the $avg accumulator return when used in a MongoDB aggregation?
AThe maximum value in the group
BThe total count of documents
CThe sum of all values
DThe average of numeric values in the group
Which MongoDB aggregation stage commonly uses $avg?
A<code>$match</code>
B<code>$group</code>
C<code>$project</code>
D<code>$sort</code>
What will $avg return if the group has no documents?
Anull
B0
CAn error
DInfinity
Can $avg be used to average string values?
AYes, it converts strings to numbers automatically
BOnly if used with <code>$toDouble</code>
CNo, it only works with numeric values
DYes, but only if strings contain digits
Which of these is a correct use of $avg in an aggregation pipeline?
A{ $group: { _id: "$category", avgPrice: { $avg: "$price" } } }
B{ $match: { avgPrice: { $avg: "$price" } } }
C{ $sort: { avgPrice: { $avg: "$price" } } }
D{ $project: { avgPrice: { $avg: "$price" } } }
Explain how the $avg accumulator works in MongoDB aggregation.
Think about how you find an average in real life.
You got /4 concepts.
    Describe a simple example of using $avg to find the average score from a collection.
    Imagine you want the average test score of all students.
    You got /4 concepts.