Introduction
Whereas I wanted to perform a quick demo to show some MongoDB useful command to mass
update some fields (Increment),I had a MongoDB error: “Cannot increment with a non-numeric argument”
I guess the message is explicit:
I can’t update this field as it is not a numeric field
Identify the field type:
As I have created my document with basic commands without specifying each field type, the fields are set by default (undefined )
How to know the type of your field?
For my example I have this list of players, their name, and also their number of goals.
When I wanted to increment the “goals” field by 10 it displayed the famous issue.
Let’s check the goals field type:
For that we will use the below command:
typeof db.collection.findOne().field_name
selecao_cracks> typeof db.selecao_cracks.findOne().Lastname
Here we see that Lastname field is undefined
If we take a look we see that Nickname field is a string type (I made some tests before)
The $TYPE operator:
The $type
operator accepts string aliases for the BSON types in addition to the numbers corresponding to the BSON types
Update the type of the field
Use the $set operator and the $toInt operator to update the field ( In fact in that case the $set operator will replace the field and its type, later we will see that we can use another operator name $convert )
selecao_cracks> db.selecao_cracks.updateMany( {}, [{ $set : { "goals":{$toInt:"$goals"}}} ]);
- Check if the field is updated
Increment the field to check if the command is now valid
selecao_cracks> db.selecao_cracks.updateMany( {}, { $inc : { goals : 10 } });
- Now let’s check if update is done
That’s it update worked and we don’t have the error related to the data type 🙂
The other operators
We have also other operator to change our field type such as:
$toString
As it’s name says it is to change the field to a string type
$toDate
this operator allows you to modify the field as a date type
Example:
- We check the data type:
- Update the field
ballon_d_or_winner> db.ballon_d_or_forgotten.updateOne( {"name":"Zico"}, [{ $set : { "date":{$toDate:"$date"}}} ]);
- The output shows that it worked
- We see that the format is note an ISODate format
$Convert
For converting the data type of a field in MongoDB.
More information related to $Convert on the MongoDB site
This operator is very effective and can convert a field to any data type, or it can convert a field to null if the conversion is not possible.
$convert
has the following syntax:
{
$convert:
{
input: ,
to: ,
onError: , // Optional.
onNull: // Optional.
}
}
In addition to $convert
, MongoDB provides the following aggregation operators as shorthand when the default “onError” and “onNull” behavior is acceptable:
Conclusion
Now you know how to update a field with the correct type especially when you need to performs tasks using integer values.
I hope it helped, next time we will go deeper with these kind of operator especially combined with aggregations as it is a huge topic with many use cases.
Don’t hesitate to check my other blogs and share with dbi bloggers.