PlanExecutor error during aggregation :: caused by :: $add only supports numeric or date types, not string

" MongoServerError[TypeMismatch]: PlanExecutor error during aggregation :: caused by :: $add only supports numeric or date types, not string" error is returned for the next aggregation, although “birth year” is of Int32 type.
db.trips.aggregate( {$project: {_id:0, "birth year":1, "newColumn": {$add: [ "$birth year",1 ] } } } )

Based on the sample_training database provided by MongoDB.

Hi @Viktor_Ushtan, welcome to the Community Forums

It wouldn’t be a training database without some lessons within it.

In this case it can be found that the types of birth year are mixed in the dataset:

sample_training> db.trips.aggregate([{$sortByCount:{$type:'$birth year'}}])
[ { _id: 'int', count: 8011 }, { _id: 'string', count: 1989 } ]

Now there can be discussions on whether or not to have a consistent type for this field, how to update them and how to enforce consistency to a set of rules if that is desired.

3 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.