MongoDB批量更新和批量插入的方式

时间:2024-03-11 19:10:46

  最近,在调试代码中发现向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 };