Chapter 2 Lab : Bringing it all together

Well, I am going at this for at least two hours and I cannot figure it out. I am getting the right query statement but the numbers don’t make sense. Need a second pair of eyes to figure what I’m doing wrong. Below is a screenshot of the result which generated by the lines below it. The problem I have is that “normalized_rating” (which is the an average of the “rating” and the “scaled_votes”, or so it says in the instructions) is showing as the “rating”; the average should be the addition of the “rating” plus the “scaled_votes” divided by 2. So either there is something wrong in my statement or the $avg operator is messed up. BTW, I tried replacing the average with a simple division (it’s only 2 values) and the result of the “normalized_rating” is null. I am at a loss! Please help…

{  
    $addFields: { 
        rating: "$imdb.rating", 
        votes: "$imdb.votes"
    } 
},
{ 
    $project: { 
        _id:0, 
        title: 1,
        rating: 1, 
        votes: 1, 
        year: 1,
        scaled_votes: {
            $add : [
                1, 
                {
                    $multiply: [
                        9,
                        {
                            $divide: [
                                {$subtract: ["$votes", 5]},
                                {$subtract: [1521105, 5]}
                            ]
                        }
                    ]
                }
            ]
        },
        normalized_rating: {
           $avg: ["$scaled_votes", "$rating"]
        }  
    } 
},

How did your $avg looked like before you modified it

Please check this link.May help

Lab - Bringing it all together

I believe it is because you are creating the field “scaled_votes” and trying to use it in a calculation in the same stage of the pipeline.
What is happening is the “scaled_votes” field is being created correctly but when the “normalized_rating” field is trying to be calculated “$scaled_votes” evaluates as null. So when $avg tries to average “$rating” and null it just returns $rating as null doesn’t count as an element; ($rating + null) / 1 => $rating.
I’d suggest cutting your definition for “scaled_votes” and pasting that into the $avg field.

Something like:

normalized_rating: {
  $avg: [
    $rating,
    {
      $add: [
        1,
        {
          $multiply: [
            9,
            {
              $divide: [
                { $subtract: [ "$votes", 5 ] },
                { $subtract: [ 1521105, 5 ] }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Hope this helps.

2 Likes

Thanks. I was thinking about the same thing last night and was going to try it this morning. I figured that if the average was the same as the rating value, then the other value was evaluated as null, or not computed yet, so perhaps it needed to be moved to yet another stage. Oh well… Coming from SQL world makes it a bit challenging. :face_with_monocle:

Thanks again.

Hi @Michael_Tantos,
You’ve forgotten the quotes in $rating. So it looks like this:

normalized_rating: {
      $avg: [
        "$rating",
        {
          $add: [
            1,
            {
              $multiply: [
                9,
                {
                  $divide: [
                    { $subtract: [ "$votes", 5 ] },
                    { $subtract: [ 1521105, 5 ] }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }

This thread really helped me figure out the normalization part of this lab. However, I noticed that if I used the following code instead of the normalization code above, I still arrive at the same answer.
avg_rating: {$avg: ‘$imdb.rating’}

???

Hi @Serissa_McAnally,

I have reached out to you individually on the forum message thread. Please share your aggregation pipeline there and we can discuss in detail.

Let me know if you have any questions.

Kind Regards,
Sonali

1 Like

A post was split to a new topic: Discussion related to Lab: Bringing it all together