Why does my MongoDB group query return always 0 in float conversion? How to fix it?

MongoDBDatabaseBig Data Analytics

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 } }
raja
Published on 13-May-2020 07:25:59
Advertisements