MongoDB - String转换为Int,并更新到数据库中
方法1 使用$convert, MongoDB版本 >= 4,速度快。
使用pymongo示范,原生mongo语句并没有尝试。
# 假设{'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)
方法2 逐个转换,速度慢,兼容各版本MongoDB。
使用原生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);
});
使用pymongo,在python层进行类型转换
import time
import pymongo
start_time = time.time()
handler = pymongo.MongoClient().db_name.collection_name
for row in handler.find({}, {'salary': 1}):
salary = int(row['salary'])
handler.update_one({'_id': row['_id']}, {'$set': {'salary': salary}})
end_time = time.time()
print('耗时:', end_time - start_time)
方法3 使用插入代替更新,速度快
相当于新建一个新的collection,然后删除原本的collection。因为是insert_many,所以速度快。经过测试,db.find()和xxx_many(insert_many、update_many)速度都很快。所以有一个前提:MongoDB中批量操作比逐个操作快多了。
以下操作不但做转换操作,还做了每个salary都加上100
使用pymongo示范
import time
import pymongo
start_time = time.time()
db = pymongo.MongoClient().db_name
old_collection = db.old_collection
new_collection = db.new_collection
new_people_info_list = []
for row in old_collection.find():
salary = int(row['salary'])
new_salary = salary + 100
new_people_info_list.append(row)
new_collection.insert_many(new_people_info_list)
end_time = time.time()
print('耗时:', end_time - start_time)
参考
- https://docs.mongodb.com/manual/reference/operator/aggregation/convert/#example
- https://stackoverflow.com/questions/4973095/how-to-change-the-type-of-a-field
MongoDB - String转换为Int,并更新到数据库中的更多相关文章
- string[]转换为int[]
今天碰到一个问题,要把string[]转换为int[],但是又不想使用循环转换,找了好久最后找到了这种方法,特此记录下. string[] input = { "1", " ...
- PowerDesigner如何将设计的表更新到数据库中
前言: 软件开发的过程中,将设计的表更新到数据库中是一件繁琐的事情,使用好工具,能够事半功倍. 环境介绍:Oracle 11g x64 前期准备: 1.PowerDesigner工具(本人是32位的) ...
- string[] 转换为 int[]
string[] ke=...... int[] output = Array.ConvertAll<string, int>(ke,delegate (string s) { retur ...
- oracle不小心更新了数据库中的所有内容后的恢复
开发过程中,在更新数据库中的某一条数据时,由于疏忽忘记加where判断条件了,这时更新会提示是否要更新全部数据,但是由于自己没有仔细看提示导致直接点确定并commit了,导致数据库中所有数据的相关字段 ...
- 一个简单的批量更新oracle 数据库中 最近的服务商名称的数据
有一个需求是这样的,我们需要更新数据库中的数据,数据时这样的 1.大约50万以上 2. 数据中有较多的重复数据 3. 需要将表中最近的代理商的名称赋值给行中的服务商名称 4. 代理商的名称可能有多个, ...
- php string转换为int
本身 var_dump : string(3) "002" 本身 is_numeric : bool(true) 本身 转换为数字 : int(2) 本身 转换为数字变量 : in ...
- 如何将String转换为int
1. int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String],[int radix]); Integer.parseIn ...
- valueof这个万能方法,将string转换为int或者int转换为string都可以
private static String testString = "111"; int stringInt = Integer.valueOf(testString); Str ...
- Python 实int型和list相互转换 现把float型列表转换为int型列表 把列表中的数字由float转换为int型
第一种方法:使用map方法 >>> list = [, ] #带有float型的列表 >>> int_list = map(int,list) #使用map转换 & ...
随机推荐
- ARM微处理器中支持字节、半字、字三种数据类型,地址的低两位为0是啥意思?
问题: ARM微处理器中支持字节.半字.字三种数据类型,其中,字需要4字节对齐(地址的低两位为0).半字需要2字节对齐(地址的最低位为0).我想问的是括号中的内容是什么意思呢?请牛人帮忙解释一下!谢谢 ...
- 【UWP】在 UWP 中使用 Exceptionless 进行遥测
2020年1月17日更新: nightly build 版本已发布 https://www.myget.org/feed/exceptionless/package/nuget/Exceptionle ...
- Android数据存储之Application
Application是Android的一大组件,在APP运行过程中有且仅有一个Application对象,它类似于javaweb中的session,贯穿整个生命周期. Application中适合保 ...
- Spark学习之路 (十)SparkCore的调优之Shuffle调优[转]
概述 大多数Spark作业的性能主要就是消耗在了shuffle环节,因为该环节包含了大量的磁盘IO.序列化.网络数据传输等操作.因此,如果要让作业的性能更上一层楼,就有必要对shuffle过程进行调优 ...
- gulp常用插件之gulp-rev使用
更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-rev这是一款为静态文件随机添加一串hash值, 解决cdn缓存问题, a.css --> a-d2f3f35d3.css.根据静 ...
- Disharmony Trees HDU - 3015 树状数组+离散化
#include<cstdio> #include<cstring> #include<algorithm> #define ll long long using ...
- 关于MySQL数据库中null的那些事
在mysql数据库中,null是一个经常出现的情况,关于mysql中的null,有哪些注意事项呢?下面简单总结归纳下,后续会不断补充. 1. is null 首先判断数据库中某一列的值是否为null, ...
- 树莓派3B从装系统到安装RYU过程
1.首先安装的是2019-09-26-raspbian-buster-lite系统 2.更换成国内源 更改apt源 sudo nano /etc/apt/sources.list(更改apt源) de ...
- pandas 将多个dataframe保存为一个excel文件的多个sheet表中
# 创建文件 def create(): df1 = pd.DataFrame({"a1": [1, 2, 3], "b1": [4, 5, 6]}) df2 ...
- vConsole 让你在手机上也能轻松调试网页
有时候为了想在手机真机上对网页进行 Debug,可手机上没有 F12,用 Chrome DevTools 连接手机操作又太过复杂.VConsole 的出现,正好解决了这一痛点! (下列内容照搬一下官方 ...