MongoDB Change The Type of a field – Guidelines
Today in this article, we will learn MongoDB Change The Type of a field. There are multiple approaches to achieving the data type change as explained below,
- Change data type using Mongo Compass UI or any other UI –Manual way
- Change data type in Mongo Using Aggregation Type Expression Operators – An automated way
- Using Aggregation Operators like $toInt , $toString
- Using Aggregation Operators like $convert
We will cover various 3-4 approaches to perform the datatype change feature.
Overall, We will cover the below aspects,
The below approach discussed is very useful when you don’t want to delete or drop the mongo fields but rather want to change the type of a field.
I have below Mongo Collection where my fields are defined as below.
I have a field name called “EmpID” which is of type “string“. I want to change this field to “int” using MongoShell.
Change data type using Mongo Compass UI or any other UI –Manual way
One can easily change the data type of a field in the Mongo DB document
In Mongo Atlas click on Edit Document. Here you will get an option to choose the data type,
As shown above, the manual approach can be used as needed.
Change data type in Mongo Using Aggregation Type Expression Operators – An automated way
Please open the command prompt or Mongo shell and log in to your database using a connection string.
We will first login to MongoDB and then switch to using the required database and the collection
Command Pattern
db.<collection-name>.update( {}, [ {$set:{ "<field-name>" : {$<field type>: "$<field-name>"}}} ]
Example – Convert string to numerical values in MongoDB
Example
db.employee.update({}, [ {$set:{ "EmpID" : {$toInt: "$EmpID"}}} ], {multi:true}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
How to convert data type for all documents in Mongo DB
You can convert data type for all documents in Mongo DB using the flag Multi as true.
What is $toInt
$toInt is an Aggregation Type Expression Operator that Converts a value to an integer. If the value is null or missing, $toInt
returns null or return errors when the value can not be converted
Example – Convert a numeric value to a string in MongoDB
Command:
db.employee.update({}, [ {$set:{ "EmpID" : {$toString: "$EmpID"}}} ], {multi:true})
Example
db.employee.update({}, [ {$set:{ "EmpID" : {$toString: "$EmpID"}}} ], {multi:true})
WriteResult({ "nMatched" : 5, "nUpserted" : 0, "nModified" : 5})
What is $toString
$toString is an aggregation function that Converts a value to a string. If the value is null or missing, $
toString
returns null or return errors when the value can not be converted
Example – Convert a NumberLong to a String
Command:
db.employee.update({}, [ {$set:{ "Ssn" : {$toString: "$Ssn"}}} ], {multi:true})
In the above example, Ssn is a long number data type field that we are converting to string type.
Using Aggregation Operators like $convert
Using this option, one can easily create an aggregation pipeline using $Convert operation which lets you convert field type easily.
Example:
db.employee.aggregate(
[
{
$project:
{
result:
{
$convert: {
input: "$EmpID",
to: "string",
onError: "An error occurred for the conversion",
onNull: "Input was null or empty"
}
}
}
}
]
).pretty()
With pretty print, you can get the expression results on the screen as shown above.
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.
Thank U very Much this work like charm and slove many problems in my back-end api project
realy thanks
Thank you Rayan, Glad it helped you!
Thank you how do we do deeply nested element’s dataype change
Hi Panaa- Thank you ..glad it helped you…Please visit this article for the nested array article.