Lab - $group and Accumulators

Could use a hint on getting started with this one.

db.movies.aggregate([
  {
    $project: {
      _id: 0,
      highest: { $max: "$imdb.rating"}
    }
  }
])

^ First, just trying to get the max imdb rating of the entire collection. Thought this query would do it, but it’s returning the imdb rating for every movie in the collection.

db.movies.aggregate({ $group : { _id: null, max: { $max : "$imdb.rating" }}});

^ Then tried a group just for the heck of it, and the result is an empty string:

{ "_id" : null, "max" : "" }

Seems like this should be straight-forward. :man_shrugging:

Hi @adam :slight_smile:

Group operates over documents, project over fields.

$max docs here

1 Like

@adam First you need to eliminate documents where imdb.rating is empty like: ‘’ before you can apply $group pipeline.

[
{
$match: {‘imdb.rating’: { $ne: ‘’ }}
},
{
$group: {
_id: null,
‘highest’: {’$max’: ‘$imdb.rating’}
}}
]

Now, you should see result of this query something like:

{"_id": null, “highest”: 9.6}