使用pymongo示范
# 假设{'age': '47'}, 转换后为{'age': 47}
import time
import pymongo
start_time = time.time()
handler = pymongo.MongoClient().db_name.collections_name
handler.update_many({}, [{'$set': {'age': {'$convert': {'input': '$age', 'to': 'int'}}}}])
end_time = time.time()
print('耗时:', end_time - start_time)
使用原生mongo语句示范(在robo3T或者在命令行上输入)
# 假设{'salary': '123'}, 转换后为{'salary': 123}
db.getCollection("collection_name").find({salary: {$exists: true}}).forEach(function(obj) {
obj.salary = new NumberInt(obj.salary);
db.db_name.save(obj);
});
MongoDB - String转换为Int,并更新到数据库中
原文:https://www.cnblogs.com/allen2333/p/11440877.html