最近,在調試代碼中發現向MongoDB插入或者更新文檔記錄時若是多條的話都是采用for循環操作的,這樣的處理方式會造成數據操作耗時,不符合批量處理的原則;對此,個人整理了一下有關MongoDB的批量更新和批量插入的操作流程,如下所示:
@Autowired
private MongoTemplate mongoTemplate;
(1)批量插入示例如下:
List<Object> insertDataList; BulkOperations operations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, collectionName); insertDataList.forEach((key, value) -> { operations.insert(mongoData) })
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 < ; 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
};
20191126閃