$convert is not allowed or the syntax is incorrect in MongoDB playground

45 Views Asked by At

I'm new to the MongoDB, and attempting to convert an age field from string to int. However, using the $convert function, I get an error stating that $convert is not allowed or the syntax is incorrect.

My code is as below:

db['1947'].aggregate(
    [
        {
          $convert: {
            input: "$age",
            to: "int",
            onError: "An error occurred",
            onNull: "Input was null or empty"
          }
        }
    ]
)

The following code is working and providing me with the expected output:

db['1947'].aggregate(
    [
      {
        $project:
          {
            _id: 0,
            result:
            {
              $convert: {
                input: "$age",
                to: "int",
                onError: "An error occurred",
                onNull: "Input was null or empty"
              }
            }
          }
      }
    ]
)

Does this mean that I cannot change the dataset from the playground? If so, how would I go about converting the age field?

0

There are 0 best solutions below