- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert string to numerical values in MongoDB?
You need to use some code in order to convert a string to numerical values in MongoDB.
Let us first create a collection with a document. The query to create a collection with a document is as follows:
> db.convertStringToNumberDemo.insertOne({"EmployeeId":"101","EmployeeName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f56528d10a061296a3c31") } > db.convertStringToNumberDemo.insertOne({"EmployeeId":"1120","EmployeeName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f56648d10a061296a3c32") } > db.convertStringToNumberDemo.insertOne({"EmployeeId":"3210","EmployeeName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f566e8d10a061296a3c33") }
Display all documents from the collection with the help of find() method. The query is as follows −
> db.convertStringToNumberDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c7f56528d10a061296a3c31"), "EmployeeId" : "101", "EmployeeName" : "Larry" } { "_id" : ObjectId("5c7f56648d10a061296a3c32"), "EmployeeId" : "1120", "EmployeeName" : "Mike" } { "_id" : ObjectId("5c7f566e8d10a061296a3c33"), "EmployeeId" : "3210", "EmployeeName" : "Sam" }
Here is the query to convert a string to numerical values in MongoDB. Change “EmployeeId” string to numerical values. The query is as follows −
> db.convertStringToNumberDemo.find().forEach(function(x) ... { ... db.convertStringToNumberDemo.update ... ( ... ... { ... "_id": x._id, ... ... }, ... { ... "$set": ... { ... "EmployeeId": parseInt(x.EmployeeId) ... } ... } ... ) ... } ... );
Now check the documents from the collection. The query is as follows −
> db.convertStringToNumberDemo.find().pretty();
The following is the output displaying the string values (EmployeeId) converted to an integer −
{ "_id" : ObjectId("5c7f56528d10a061296a3c31"), "EmployeeId" : 101, "EmployeeName" : "Larry" } { "_id" : ObjectId("5c7f56648d10a061296a3c32"), "EmployeeId" : 1120, "EmployeeName" : "Mike" } { "_id" : ObjectId("5c7f566e8d10a061296a3c33"), "EmployeeId" : 3210, "EmployeeName" : "Sam" }
- Related Articles
- How to convert ObjectId to string in MongoDB
- Convert list of numerical string to list of Integers in Python
- Convert NumberLong to String in MongoDB?
- Convert string to objectid in MongoDB?
- How to convert value to string using $toString in MongoDB?
- How to convert from string to date data type in MongoDB?
- MongoDB query to convert string to int?
- Converting isodate to numerical value in MongoDB?
- How to convert multiple numerical variables to factor variable in R?
- MongoDB query to convert numeric string to number
- MongoDB query to convert from ObjectId to String
- How to convert a numerical column into factor column in R?
- How to concatenate numerical vectors and a string to return a string in R?
- Convert a string to hexadecimal ASCII values in C++
- How to convert dot to comma for numerical columns in an R data frame?

Advertisements