最近,在调试代码中发现向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批量更新和批量插入的方式的更多相关文章

  1. Hibernate批量更新和批量删除批量添加(转)

    通常,在一个Session对象的缓存中只存放数量有限的持久化对象,等到Session对象处理事务完毕,还要关闭Session对象,从而及时释放Session的缓存占用的内存.批量处理数据是指在一个事务 ...

  2. mysql进阶(十四) 批量更新与批量更新多条记录的不同值实现方法

    mysql 批量更新与批量更新多条记录的不同值实现方法 在mysql中批量更新我们可能使用update,replace into来操作,下面详细介绍mysql批量更新与性能. 批量更新 mysql更新 ...

  3. mysql 批量更新和批量插入

    1. 批量更新 update table_name set field_name = CASE id WHEN id1 THEN  field_value, WHEN id1 THEN  field_ ...

  4. MySQL进行 批量插入,批量删除,批量更新,批量查询

    1.批量插入 ServiceImpl层 List<Person> addPeople = new ArrayList<>(); //addPeople存放多个Person对象 ...

  5. mysql 批量更新与批量更新多条记录的不同值实现方法

    批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 代码如下: UPDATE mytable SET myfield = 'value' WHERE other_field = ...

  6. LightSpeed的批量更新和批量删除

    1.Update对于批量操作 无论是Update还是Remove  都是使用LightSpeed的Query对象来完成. 注:Student是要进行Update的表(实体),StuName是表Stud ...

  7. 批量更新sql |批量update sql

    图所示现需要批量更新table2表内字段Pwd更新userName对IP地址username与Ip对应关系table1所示 update table2 set pwd=table1.ip from t ...

  8. mysql 批量更新与批量更新多条记录的不同值实现方法

    作者: 字体:[增加 减小] 类型:转载 时间:2013-10-02 我要评论 在mysql中批量更新我们可能使用update,replace into来操作,下面小编来给各位同学详细介绍mysql ...

  9. 【转】【MySql】Update批量更新与批量更新多条记录的不同值实现方法

    批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: UPDATE mytable SET myfield = 'value' WHERE other_field = 'other ...

随机推荐

  1. bitmap+文本生成新的bitmap的实现

    注:参数content为生成二维码bitmap的内容,该二维码bitmap在和文本title组合生成一个新的bitmap package info.ecloud.merchant.util; impo ...

  2. 「Flink」RocksDB介绍以及Flink对RocksDB的支持

    RocksDB介绍 RocksDB简介 RocksDB是基于C++语言编写的嵌入式KV存储引擎,它不是一个分布式的DB,而是一个高效.高性能.单点的数据库引擎.它是由Facebook基于Google开 ...

  3. 802.11有线等效加密WEP

    有线等效加密(WEP)标准是802.11无线安全早期的解决方案,WEP并不安全. 既然WEP并不安全,为什么还要学习WEP呢? WEP简单,相比后续出现的加密协议,它不要求有多么强大的计算能力.一些老 ...

  4. 浅析设计模式之mvc、mvp、mvvm

    mvc.mvvm.mvp是常见的设计模式,也是常见的设计思想,现对它们进行简要的归纳总结 三种模式的介绍 1.MVC:经典设计模式 View 传送指令到 Controller(控制器) Control ...

  5. MySql 部分字段去重

    select * from personal_question_answer where answer_id in ( select min(answer_id) from personal_ques ...

  6. Oracle v$session视图显示客户端IP地址

    在Oracle数据库中,我们使用session相关视图(v$session.v$active_session_history,dba_hist_active_session_history等)查找问题 ...

  7. 吴裕雄--天生自然 R语言数据可视化绘图(1)

    par(ask=TRUE) opar <- par(no.readonly=TRUE) # make a copy of current settings attach(mtcars) # be ...

  8. python3的bytes数据类型

    python已升级到了3.0,都说现在是属于python3,未来也是属于python3,那python3到底改了些什么呢? 其中我记得非常清楚的是,python3对文本和二进制数据作了更为清晰的区分. ...

  9. css基础-盒子模型+背景和列表

    border-style的值: none 无 dotted 点状 dashed 虚线 solid 实线 double 双实线 margin: 垂直方向两个相邻元素都设置了外边距,那么外边距会发生合并 ...

  10. IntelliJ IDEA 2018.3.2 永久破解

    PS:动手能力强的来,手残的去淘宝买吧,大概15块钱1年.建议看完后在动手,有一个全局观,浪费不了多少时间 一. 下载破解补丁文件 链接:https://pan.baidu.com/s/1wFp14t ...