Chapter 3: Lecture 1 - unnecessary $match

Hi,
there seems to be an unnecessary $match stage at the very end of the lecture:
{ "$match": { "metacritic": { "$gte": 0 }}}

as the following $avg accumulator function ignores non-numeric values.

I think it should be noted. Doing this without $match makes the same results and is confusing.
Asking for a friend .-)

Good catch there @Peter_79812

But… the documentation also states:

Let’s test it out:

// 1. Insert test records
db.avg_coll.insertMany([
   { "item" : "abc", "price" : 5 },
   { "item" : "abc", "price" : "ignore abc" },
   { "item" : "def", "price" : 0 },
   { "item" : "def", "price" : "ignore def" },
   { "item" : "jkl", "price" : 20 },
   { "item" : "jkl", "price" : 45 },
   { "item" : "mno", "price" : "ignore mno" },
   { "item" : "mno", "price" : "ignore mno" },
   { "item" : "xyz", "price" : null },
   { "item" : "xyz", "price" : null }
])


// 2. Calculate averages and sort
db.avg_coll.aggregate([
   {
      $group: {
         _id: "$item",
         avgPrice: { $avg: "$price" }
      }
   },
   {
      $sort: {
		  "_id": 1
	  }
   }
])


// 3. Result
{ "_id" : "abc", "avgPrice" : 5 }
{ "_id" : "def", "avgPrice" : 0 }
{ "_id" : "jkl", "avgPrice" : 32.5 }
{ "_id" : "mno", "avgPrice" : null }
{ "_id" : "xyz", "avgPrice" : null }

So really, it could be re-worded to something along the lines of…

$avg treats non-numeric values as null. Null values are not included as part of the calculation but will result in a null average if all of the operands for the average are non-numeric/null."

And in most cases, you probably don’t want to see Null averages hence the explicit exclusion in the lecture.

2 Likes