@AutowiredprivateMongoTemplate mongoTemplate;
(1)批量插入示例如下:
ListinsertDataList;
BulkOperations operations=mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, collectionName);operations.insert(insertDataList);
BulkWriteResult result=operations.execute();
(2)批量修改示例如下:
ListupdateDataList;
BulkOperations operations=mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, collectionName);
updateDateList.forEach(date->{
Query queryUpdate= newQuery();
queryUpdate.addCriteria(where("_id").is(value));
Update update= newUpdate();
update.set(field1, value1).set(field2, value2);
operations.updateOne(queryUpdate, update);
});
BulkWriteResult result=operations.execute();
(3)利用BulkOperations的upsert方法可以同时支持插入和更新操作,示例如下:
List dataList = new ArrayList<>();
List> updateList = new ArrayList<>(dataList.size());
BulkOperations operations=mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, collectionName);
dataList.forEach(data->{
Query query= new Query(newCriteria(field1).is(value1)).addCriteria(newCriteria(field2).is(value2));
Update update= newUpdate();for (int index = 0; index < dataList.size(); index++) {
String key=data.getKey();
String value=data.getValue();
update.set(key, value);
}
Pair updatePair =Pair.of(query, update);
updateList.add(updatePair);
});
operations.upsert(updateList);
BulkWriteResult result=operations.execute();
备注:BulkOperations.BulkMode.UNORDERED 和 BulkOperations.BulkMode.ORDERED的区别:
UNORDERED是平行处理,即使某条记录出错了,其余的也会继续处理;
ORDERED是队列排序处理,只要中途有个失败了,那么后续的操作流程就会终止了。enumBulkMode {/**Perform bulk operations in sequence. The first error will cancel processing.*/ORDERED,/**Perform bulk operations in parallel. Processing will continue on errors.*/UNORDERED
};