I have a >6M documents collection in mongodb. And one of it’s fields (field1 and field2 in the example below) are string values (type 2 in mongodb).
My problem is that I want to parse them into float values (all the values are parseFloat-able). I found this snippet in SO. But it doesn’t seems to be a great solution to deal with a 6M document collection.
db.collection.find({field1: {$type:1}}).forEach(function(data) {
db.collection.update(
{_id:data._id},
{$set:{
field1: parseFloat(data.field1),
field2: parseFloat(data.field2)}
}
)
})
Is there any way I can convert my two fields without slowing down the server ?
Using db.collection.getIndexes() and db.collection.getIndexKeys() says that my two fields are indexed.
If your goal is to prevent server slowdown then I would introduce a sleep on the client end between updates. You can adjust the timeout depending on how much load you want to reduce and your patience for the updates to complete. To force a sleep call “sleep(ms)” in the mongo shell where “ms” is the number of milliseconds you would like to sleep for.