Aggregation / Chapter 3. Lab - $group and Accumulators - Error using accumulator stages

Hi there. In this lab ask to filter if the movie won oscars, and then use accumulator stages to see max , min rates and so on.

I’m using this code:

db.movies.aggregate( [
{stage1 -$project => check if first 3 words is Won, return true if is (works ok!)} ,
{stage2 => match Won = true (works ok} ,
{stage3 => $project accumulator stage for rate ($max, $min, etc) => doesnt work , returning the same rate value, doesnt not calculate max min or whatever } ]}

This is the resulting of stage1 and stage2:

{ “title” : “7th Heaven”, “oscar_awards” : true, “rate” : 7.8 }
{ “title” : “Sunrise”, “oscar_awards” : true, “rate” : 8.4 }
{ “title” : “Wings”, “oscar_awards” : true, “rate” : 7.8 }
{ “title” : “The Last Command”, “oscar_awards” : true, “rate” : 8 }
{}
] )

For me, doing the next stage as an accumulator should be working but it doesnt, returning the same rate as result. I’m missing something?

Thanks.

Hi @Rafael_Barlera_Alves, welcome to the community.
You can use the following Regex syntax to search for documents for the appropriate text stating they won an Oscar:

    $match: {
      awards: /Won \d{1,2} Oscars?/
    }

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Ok @SourabhBagrecha . I using your expression now, but the error persists. I’m going to post what im doing, sorry to being explicit about my code.

I’m putting your $match query and add a {$project:{‘imdb.rating’:1 , ‘_id’:0 }}

returns:

{ “imdb” : { “rating” : 7.8 } }
{ “imdb” : { “rating” : 8.4 } }
{ “imdb” : { “rating” : 7.8 } }
{ “imdb” : { “rating” : 8 } }
{ “imdb” : { “rating” : 7.5 } }

and now i changed my project to: {$project:{max_rate:{$max :’$imdb.rating’}, _id:0 }}

and is returning:

{ “max_rate” : 7.8 }
{ “max_rate” : 8.4 }
{ “max_rate” : 7.8 }
{ “max_rate” : 8 }
{ “max_rate” : 7.5 }

At this project, shouldn’t bee returning the max value?

What is the question mark doing in the regex? it was what i was missing to get the correct answer.

Hi @Cesar_Tolentino, welcome back to the community. :wave:
As per the regular expression guide on question mark:

The question mark makes the preceding token in the regular expression optional. colou?r matches both colour and color. The question mark is called a quantifier.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like