Chapter 3: Core Aggregation - $group and Accumulators

The answer return empty. What did I do wrong?

var pipeline = [

    Won 13 Oscars,

    Won 1 Oscar

]

db.movies.aggregate([
    { $match: {
        "awards": {
            $regex: /Won \d+ Oscar/
        }
    }},

    { $group: {
        _id: null,
        "highest_rating": {$max:"$imdb.rating"},
        "lowest_rating": {$min: "$imdb.rating"},
        "average_rating": {$avg: "$imdb.rating"},
        "deviation": {$stdDevPop: "$imdb.rating"},
        "awards.wins": {$in: pipeline}
    }}
])

Hi @Angelica_Shigematsu, you are trying to do a regular expression match on the awards field which is an array and therefor nothing matches which is why you get an empty result. I think what you might be looking for is a match on the awards.text field. Making that change should get you a result.

With that change I have the following aggregate command:

db.movies.aggregate(
    [
        {
            "$match": { "awards.text": {"$regex": /Won \d+ Oscar/}}
        },
        {
            "$group": {
                "_id": null,
                "highest_rating": {"$max":"$imdb.rating"},
                "lowest_rating": {"$min":"$imdb.rating"},
                "average_rating": {"$avg": "$imdb.rating"},
                "deviation": {"$stdDevPop": "$imdb.rating"},
            }
        }
    ]
)

And this returns the following document:

{
  _id: null,
  highest_rating: 9.2,
  lowest_rating: 4.5,
  average_rating: 7.531803628601922,
  deviation: 0.5969794583664374 
}
2 Likes

This worked. Thank you.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.