M121: Bringing it all together sort issue - parse error

Hi,
I’m new and struggling on this lab and am confused. I can project the output after converting rating and votes and calculating an avg from them as well as scaling based on the handout. When I add a sort to try and find the lowest value, I get the below error, which makes no sense as when I remove the sort stage, it prints out values…
Help please and thank you.

Error message:

errmsg : Failed to parse number ‘’ in $convert with no onError value: No digits,
code : 241,
codeName : ConversionFailure,

Wade

{ “title” : “American Ninja 4: The Annihilation”, “year” : 1990, “normalized_votes” : 1.0188271645519689 }

{ “title” : “Guns”, “year” : 1990, “normalized_votes” : 1.0034672276641905 }

{ “title” : “Metamorphosis”, “year” : 1990, “normalized_votes” : 1.002893300900664 }

{ “title” : “Initiation: Silent Night, Deadly Night 4”, “year” : 1990, “normalized_votes” : 1.0051653408717376 }

{ “title” : “Solar Crisis”, “year” : 1990, “normalized_votes” : 1.0080941423969496 }

{ “title” : “Beyond Darkness”, “year” : 1990, “normalized_votes” : 1.0015442771678391 }

Hi @Wade_Baldwin, welcome to the community.
Can you share your aggregation pipeline with us?
Don’t forget to wrap it with spoiler tags as below.
[spoiler] <------Your pipeline here ----> [/spoiler]

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Hi @SourabhBagrecha sure thing and thank you I’ve added it below.
As I mentioned in previous post, if I remove the sort, it provides me with output.

Wade

db.movies.aggregate([
    {

        $addFields: {
             rating: { $toInt: "$imdb.rating" },
             votes: { $toInt: "$imdb.votes"}
       }
      },
      {
          $match: {
              languages: ["English"],
              year: {$gte:1990},
              rating: {$gte:1},
              votes: {$gte:1}
          }
      },
    {
        $project:{_id: 0, title: 1, year: 1,
        normalized_votes: {
        $add: [
      1,
      {
        $multiply: [
          9,
          {
            $divide: [
              { $subtract: ["$votes", 5] },
              { $subtract: [1521105, 5] }
            ]
          }
        ]
      }
    ]
  }
}
},
{
    $sort: {normalized_votes:-1}
}
]) 

You should do the $match before the $addFields for 2 reasons:

  1. Pruning non matching documents ensure you $addFields is only performed on interesting documents, which is faster.

  2. You are calling $toInt on fields that might not exist. This may certainly cause to $convert error you have since $toInt is a short-cut of $convert.

Now, why do you get the error with $sort while you do not get it without? It is because with the sort, all documents must be processed before outputting the first one. Without the sort as soon as a document can be outputted it is. Since you have a cursor the bad documents (the one without imdb.rating or imdb.votes) are not yet processed even if you have some output.

Was $toInt conversion part of the lab requirement?

1 Like

Thanks @steevej
Will try the match first and I was getting a “text” field error without the $toint (was not part of the lab), which is why I was converting the elements and got some hints from other forum posts for the same. If they don’t need to be converted it would make it much simpler. I wasn’t sure how to use a if:null argument to avoid empty fields, but maybe that will be resolved if I use the match first. Will come back if I still can’t solve.

Wade

1 Like