Mongo writes in quotes, causing some numeric fields to be of type string and changing the field type to numeric.

db.Report99.find().forEach( function(doc){ db.Report99.update({'_id': doc._id},{$set:{"begin-quantity": parseFloat(doc["begin-quantity"])}}); / / to floating-point db. Report99. Update ({" _id ": doc. _id}, {$set: {" end - quantity" : NumberInt (doc/" end - quantity ")}}); });Copy the code

Mongo4.2 + version, parameters can be as follows:

db.getCollection("my-report").update( { "begin-quantity": { $type: "string" } }, [{ $set: { "begin-quantity": {$convert: {input: "$business - quantity,", "double"}}}}], / / effective type: string|bool|int|long|double|decimal|date|timestamp|objectId ... { multi: true } )Copy the code

Description:

1, if the collection name contains special characters, use db.getCollection('my-report')

ForEach ['']; forEach ['']; forEach ['']

This article is published by OpenWrite!