mongo写入加了引号,导致一些数值字段都是string类型,修改字段类型为数值型。
db.Report99.find().forEach(function(doc){db.Report99.update({'_id': doc._id},{$set:{"begin-quantity": parseFloat(doc["begin-quantity"])}}); //改为浮点数db.Report99.update({'_id': doc._id},{$set:{"end-quantity": NumberInt(doc["end-quantity"])}}); // 改为整型});# mongo4.2版本以上db.getCollection("my-report").update({ "begin-quantity": { $type: "string" } },[{ $set: { "begin-quantity": { $convert: { input: "$average-quantity", to: "double" } } } }], //有效类型: string|bool|int|long|double|decimal|date|timestamp|objectId ...{ multi: true })
说明:
1、如果集合名包含特殊字符,使用db.getCollection('my-report')
2、forEach中doc取某个字段值可直接doc.key,如果key中包含特殊字符,使用doc['']
文章转载自我是阿魏,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




