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?✗ Incorrect
$avg calculates the average (mean) of numeric values in the group.
Which MongoDB aggregation stage commonly uses
$avg?✗ Incorrect
$avg is used inside the $group stage to calculate averages per group.
What will
$avg return if the group has no documents?✗ Incorrect
If there are no documents in the group, $avg returns null.
Can
$avg be used to average string values?✗ Incorrect
$avg only works on numeric values. Strings are ignored.
Which of these is a correct use of
$avg in an aggregation pipeline?✗ Incorrect
$avg is used inside $group to calculate averages per group.
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.