0

How can we change datatype of a field from string to double in mongodb. I had tried some code but not working.

At-present I have 28000 entries in table. I am using mongodb 2.6.10.

Any help would be appreciated.

db.booking.find({prepayment_amount: {$exists: true}}).forEach(function(obj) {
     obj.prepayment_amount = new parseDouble(obj.prepayment_amount);
     db.booking.save(obj);
    }); 

Error

When I run this code in mongo shell I am getting

> db.booking.find({prepayment_amount: {$exists: true}}).forEach(function(obj) {
...      obj.prepayment_amount = new parseDouble(obj.prepayment_amount);
...      db.booking.save(obj);
...     });
2017-08-04T09:28:04.670+0000 E QUERY    [thread1] ReferenceError: parseDouble is not defined :
@(shell):2:6
DBQuery.prototype.forEach@src/mongo/shell/query.js:501:1
@(shell):1:1
Sarath TS
  • 2,432
  • 6
  • 32
  • 79
  • define "not working" please. Is it throwing errors? What do the "strings" you are trying to convert actually look like? I also note that I know I've already given you an answer with a much more efficient loop than what you are using here. – Neil Lunn Aug 04 '17 at 09:22
  • Taking a look at a previous question of yours it would seem that the data is actually stored something like `prepayment_amount: "55"` So if you only problem here is that you are "expecting" it to show as `prepayment_amount: 55.00` then that may well be simply all about the interface you are viewing the data from. If the data is actually stored as a "double" then it will match BSON Type 1. So using [`$type`](https://docs.mongodb.com/manual/reference/operator/query/type/) `.find({ "prepayment_amount": { "$type": 1 } }).count()` should return equal to the collection count after conversion. – Neil Lunn Aug 04 '17 at 09:29
  • Or more to the point, if the job is in fact done then `.find({ "prepayment_amount": { "$type": 2 } }).count()` which is looking for "string" should return `0`. Meaning the conversion was correctly done. – Neil Lunn Aug 04 '17 at 09:30
  • Thanks for your reply. I added error response also in my question – Sarath TS Aug 04 '17 at 09:33
  • 1
    Missed that completely. Because it's `parseFloat` and not `parseDouble` since there is no such method. Or of course simply `parseInt` if there is no decimal point in any of the strings. Also you do not use `new`. – Neil Lunn Aug 04 '17 at 09:36
  • Also there is `Number(obj.prepayment_amount)` as a built in function for the shell aside from the other built in JavaScript methods. More modern mongodb releases have `NumberDecimal()` as well, but not 2.6 – Neil Lunn Aug 04 '17 at 09:38
  • @Neil Lunn, Please check and correct me `var ops = []; db.booking.find({prepayment_amount: {$exists: true}}).forEach(doc => { doc.prepayment_amount = parseFloat(doc.prepayment_amount); ops.push({ "updateOne": { "filter": { "_id": doc._id }, "update": { "$set": { "prepayment_amount": doc.prepayment_amount } } } }); if ( ops.length >= 500 ) { db.booking.bulkWrite(ops); ops = []; } }); if ( ops.length > 0 ) { db.booking.bulkWrite(ops); ops = []; }` – Sarath TS Aug 04 '17 at 10:06

0 Answers0