Sanitize data on movies collection

In chapter 3 in the lecture the data seem to be unsanitized so I tried to clean it up a bit

db.movies.aggregate([{
    $addFields: {
          year: {
             $convert: {
                  input: "$year",
                  to: "int"
         }
    }
},
{
   $group: {
       _id: {
           year: "$year",
           runtime: {
             $cond: [{ $eq: [{ $type: "$runtime" }, "int"] }, "$runtime", 0]
    }
  },
   num_films_in_years: { $sum: 1 },
   averageRuntime: { $avg: "$runtime" }
}])

In short, I want to convert all year data to number because some year data contain strange letter which make it unable to group. But execute the code above gave me an error

"errmsg" : "Unrecognized expression '$convert'"

Which seem strange to me as why it doesn’t recognize $convert.

So why this error message happen, and is there any way to fix this?

Hey @Ann_90310

From the docs on $convert

New in version 4.0.

The Mongod that is used by that Atlas Cluster is version 3.6.14; which does not have the $convert aggregate expression.

You will see this when you first connect.

mongo "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/aggregations?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl -u m121 -p aggregations --norc
1 Like

Thanks. I just realized this that I can’t use any method that new after version 4.0