
- 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
MongoDB query to convert a string with commas to double
For such conversion, use aggregate(). Let us create a collection with documents −
> db.demo335.insertOne({"Value":"45,67,78.0"}); { "acknowledged" : true, "insertedId" : ObjectId("5e522a1cf8647eb59e562091") } > db.demo335.insertOne({"Value":"17664,76,534.0"}); { "acknowledged" : true, "insertedId" : ObjectId("5e522a26f8647eb59e562092") } > db.demo335.insertOne({"Value":"8899,322,135,875.50"}); { "acknowledged" : true, "insertedId" : ObjectId("5e522a34f8647eb59e562093") } > db.demo335.insertOne({"Value":"1,533.07"}); { "acknowledged" : true, "insertedId" : ObjectId("5e522ab9f8647eb59e562094") }
Display all documents from a collection with the help of find() method −
> db.demo335.find();
This will produce the following output −
{ "_id" : ObjectId("5e522a1cf8647eb59e562091"), "Value" : "45,67,78.0" } { "_id" : ObjectId("5e522a26f8647eb59e562092"), "Value" : "17664,76,534.0" } { "_id" : ObjectId("5e522a34f8647eb59e562093"), "Value" : "8899,322,135,875.50" } { "_id" : ObjectId("5e522ab9f8647eb59e562094"), "Value" : "1,533.07" }
Following is the query to convert a string with commas to double −
db.demo335.aggregate([ ... { $project: { ... data: { ... $convert: { ... input: { ... $reduce: { ... input: { ... $split: ['$Value', ','] ... }, ... initialValue: '', ... in: { ... $concat: ['$$value', '$$this'] ... } ... } ... }, ... to: 'double', ... onError: 0 ... } ... } ... }} ... ])
This will produce the following output −
{ "_id" : ObjectId("5e522a1cf8647eb59e562091"), "data" : 456778 } { "_id" : ObjectId("5e522a26f8647eb59e562092"), "data" : 1766476534 } { "_id" : ObjectId("5e522a34f8647eb59e562093"), "data" : 8899322135875.5 } { "_id" : ObjectId("5e522ab9f8647eb59e562094"), "data" : 1533.07 }
- Related Articles
- MongoDB query to convert string to int?
- MongoDB query to convert from ObjectId to String
- MongoDB query to convert numeric string to number
- How to Convert Double to String to Double in Java?
- How to convert a currency string to a double with jQuery or JavaScript?
- Convert double to string in Java
- Convert String to Double in Java
- Java Program to convert String to Double
- Java methods to convert Double to String
- How to convert a double value to String in Java?
- Golang Program to convert string variables to double
- Convert double value to string in Java
- Convert string (varchar) to double in MySQL
- Convert a String to a double type number in Java
- How to remove double inverted commas in R matrix?

Advertisements