
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to change the type of a field in MongoDB?
Let us convert string type to int for an example. Aggregation does not allow us to directly change the type of a field; therefore, you need to write a code to convert the type of a field.
At first, create a collection with document. After that we will get the type of every field. The query to create a collection with document is as follows
>db.changeDataType.insertOne({"StudentName":"Larry","StudentAge":23,"StudentZipCode":" 10001","isProgrammer":false}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ed4976fd07954a4890694") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.changeDataType.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6ed4976fd07954a4890694"), "StudentName" : "Larry", "StudentAge" : 23, "StudentZipCode" : "10001", "isProgrammer" : false }
Now let us check type of every field in a collection. The query is as follows to check the type of every field:
> checkType=db.changeDataType.findOne(); { "_id" : ObjectId("5c6ed4976fd07954a4890694"), "StudentName" : "Larry", "StudentAge" : 23, "StudentZipCode" : "10001", "isProgrammer" : false }
Now here we will use the above variable “checkType” to get the type of every variable. The query is as follows:
> typeof checkType._id; object > typeof checkType.StudentName; string > typeof checkType.StudentAge; number > typeof checkType.StudentZipCode; string > typeof checkType.isProgrammer; boolean
Now, change the type of “StudentZipCode” from string to number type. The query is as follows:
> db.changeDataType.find().forEach(function(ch) ... { ... db.changeDataType.update({ ... "_id":ch._id}, ... {"$set": ... { ... "StudentZipCode":parseInt(ch.StudentZipCode) ... } ... }); ... });
I have changed the type of “StudentZipCode” from string to number type. To verify, let us follow what we saw above to check the type of field in a collection:
> againCheckType=db.changeDataType.findOne(); { "_id" : ObjectId("5c6ed4976fd07954a4890694"), "StudentName" : "Larry", "StudentAge" : 23, "StudentZipCode" : 10001, "isProgrammer" : false }
Use the above variable to check the type of field:
> typeof againCheckType.StudentZipCode; number
Look at the above output now, the “StudentZipCode” field have been changed from String type to number.
- Related Articles
- How to exclude array type field value in MongoDB?
- How can I change the field name in MongoDB?
- How to change the type of a column in PostgreSQL?
- Find and replace NumberLong type field in MongoDB?
- How to increment a field in MongoDB?
- MongoDB query to change simple field into an object?
- How to return only value of a field in MongoDB?
- How to calculate average of a particular field in MongoDB?
- How to make a unique field in MongoDB?
- How to insert a boolean field in MongoDB?
- How to select a single field in MongoDB?
- How to sum every field in a sub document of MongoDB?
- How to find a document by the non-existence of a field in MongoDB?
- How to limit the amount of characters returned from a field in a MongoDB?
- How to determine whether a field exists in MongoDB?
