MongoDB批量更新和批量插入的方式
最近,在调试代码中发现向MongoDB插入或者更新文档记录时若是多条的话都是采用for循环操作的,这样的处理方式会造成数据操作耗时,不符合批量处理的原则;对此,个人整理了一下有关MongoDB的批量更新和批量插入的操作流程,如下所示:
@Autowired
private MongoTemplate mongoTemplate;
(1)批量插入示例如下:
List<Object> insertDataList;
BulkOperations operations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, collectionName);
operations.insert(insertDataList);
BulkWriteResult result = operations.execute();
(2)批量修改示例如下:
List<Object> updateDataList;
BulkOperations operations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, collectionName);
updateDateList.forEach(date -> {
Query queryUpdate = new Query();
queryUpdate.addCriteria(where("_id").is(value));
Update update = new Update();
update.set(field1, value1).set(field2, value2);
operations.updateOne(queryUpdate, update);
});
BulkWriteResult result = operations.execute(); (3)利用BulkOperations的upsert方法可以同时支持插入和更新操作,示例如下:
List<T> dataList = new ArrayList<>();
List<Pair<Query, Update>> updateList = new ArrayList<>(dataList.size());
BulkOperations operations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, collectionName);
dataList.forEach(data -> {
Query query = new Query(new
Criteria(field1).is(value1)).addCriteria(new Criteria(field2).is(value2));
Update update = new Update();
for (int index = 0; index < dataList.size(); index++) {
String key = data.getKey();
String value = data.getValue();
update.set(key, value);
}
Pair<Query, Update> updatePair = Pair.of(query, update);
updateList.add(updatePair);
});
operations.upsert(updateList);
BulkWriteResult result = operations.execute(); 备注:BulkOperations.BulkMode.UNORDERED 和 BulkOperations.BulkMode.ORDERED的区别:
UNORDERED是平行处理,即使某条记录出错了,其余的也会继续处理; ORDERED是队列排序处理,只要中途有个失败了,那么后续的操作流程就会终止了。
enum BulkMode { /** Perform bulk operations in sequence. The first error will cancel processing. */
ORDERED, /** Perform bulk operations in parallel. Processing will continue on errors. */
UNORDERED
};
MongoDB批量更新和批量插入的方式的更多相关文章
- Hibernate批量更新和批量删除批量添加(转)
通常,在一个Session对象的缓存中只存放数量有限的持久化对象,等到Session对象处理事务完毕,还要关闭Session对象,从而及时释放Session的缓存占用的内存.批量处理数据是指在一个事务 ...
- mysql进阶(十四) 批量更新与批量更新多条记录的不同值实现方法
mysql 批量更新与批量更新多条记录的不同值实现方法 在mysql中批量更新我们可能使用update,replace into来操作,下面详细介绍mysql批量更新与性能. 批量更新 mysql更新 ...
- mysql 批量更新和批量插入
1. 批量更新 update table_name set field_name = CASE id WHEN id1 THEN field_value, WHEN id1 THEN field_ ...
- MySQL进行 批量插入,批量删除,批量更新,批量查询
1.批量插入 ServiceImpl层 List<Person> addPeople = new ArrayList<>(); //addPeople存放多个Person对象 ...
- mysql 批量更新与批量更新多条记录的不同值实现方法
批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 代码如下: UPDATE mytable SET myfield = 'value' WHERE other_field = ...
- LightSpeed的批量更新和批量删除
1.Update对于批量操作 无论是Update还是Remove 都是使用LightSpeed的Query对象来完成. 注:Student是要进行Update的表(实体),StuName是表Stud ...
- 批量更新sql |批量update sql
图所示现需要批量更新table2表内字段Pwd更新userName对IP地址username与Ip对应关系table1所示 update table2 set pwd=table1.ip from t ...
- mysql 批量更新与批量更新多条记录的不同值实现方法
作者: 字体:[增加 减小] 类型:转载 时间:2013-10-02 我要评论 在mysql中批量更新我们可能使用update,replace into来操作,下面小编来给各位同学详细介绍mysql ...
- 【转】【MySql】Update批量更新与批量更新多条记录的不同值实现方法
批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: UPDATE mytable SET myfield = 'value' WHERE other_field = 'other ...
随机推荐
- 吐槽一下python
关于python,优点有很多.例如,编码灵活,书写随意. 印象最深的就是,Duck Type.也就说,如果使用会走路和会飞来衡量鸭子, 那么如果一个物体,走路像鸭子,飞起来像鸭子,那么它就是鸭子. d ...
- python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件)
# python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件) import tkinter as tk from tkinter import filedial ...
- linux中vim使用技巧
一.导入文件内容 :r 解释 导入文件 示例 编辑模式下 # 将/tmp/test.txt内容导入到光标所在的位置的下一行 :r /tmp/test.txt :! 解释 在编辑文件时,执行系统命令 示 ...
- MongoDB导入导出以及数据库备份111
-------------------MongoDB数据导入与导出------------------- 用命令行打开mongo安装路径如图: 执行后, 在此处输入命令,如:mongoexport ...
- ES6 - 报错整理(1): Unexpected end of JSON input while parsing near '...es":"7.0.0-alpha.11",'
npm install --save-dev 安装 babel-preset-env时一直报错 Unexpected end of JSON input while parsing near '... ...
- 源码浅析:MySQL一条insert操作,会写哪些文件?包括UNDO相关的文件吗?
DML操作的大致流程 在解答上述疑惑之前,我们来梳理一下DML操作的大致流程: 1.语法解析.语义解析 2.生成执行计划 3.事务修改阶段 1) 激活事务,事务状态由not_active变为activ ...
- 浅谈centos8与centos7
距离centos8.0(现在已经更新到8.1了)的发布已经过去几个月了,作为一个刚刚接触过几个月centos的萌新来说,本文想通过实际的操作体验来说对比一下centos8代与7代 首先,centos8 ...
- 使用ffmpeg为影片添加字幕
ffmpeg -i infile.mp4 -i infile.srt -c copy -c:s mov_text outfile.mp4 The order of -c copy -c:s mov_t ...
- jsx中的路径拼接
<img style={{height:80,width:80}} src={"/images/"+index+ ".jpg"}/>其中 index ...
- PRML学习准备
因为很怕PRML课程,所以想提前学习下做点准备. 看的一个学习内容就是python数据处理那本书,比较仔细地学习了 numpy,大致看了pandas和 matplotlib ,有以下几点感受 nump ...