![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Why does my MongoDB group query return always 0 in float conversion? How to fix it?
For float conversion, use parseFloat() in MongoDB. Let us create a collection with documents −
> db.demo523.insertOne({"details":{values:"-0.45"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e89b7efb3fbf26334ef611f") }
Display all documents from a collection with the help of find() method −
> db.demo523.find();
This will produce the following output −
{ "_id" : ObjectId("5e89b7efb3fbf26334ef611f"), "details" : { "values" : "-0.45" } }
Following is the query that does not result 0 in the conversion of float −
>db.getCollection('demo523').find({}).forEach( function(d) ... { d.details.values = parseFloat( d.details.values ) ... db.getCollection('demo523').save(d)} );
Display all documents from a collection with the help of find() method −
> db.demo523.find();
This will produce the following output −
{ "_id" : ObjectId("5e89b7efb3fbf26334ef611f"), "details" : { "values" : -0.45 } }
Advertisements