目录
一、批量更新
java
/**
* 批量更新的操作
* @return
*/
public int batchUpdate(){
List<StudentDo> list = new ArrayList<>(); //要修改的一批数据
List<Pair<Query, Update>> updateList = new ArrayList<>(list.size());
BulkOperations operations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, "w_test1");
list.forEach(data -> {
//如果query查询到有数据就更新
Query query = new Query(new Criteria("_id").is(data.getId()));
Update update = new Update();
update.set("stu_name",data.getStuName());
update.set("updateTime",data.getUpdateTime());
Pair<Query, Update> updatePair = Pair.of(query, update);
updateList.add(updatePair);
});
operations.upsert(updateList);
BulkWriteResult result = operations.execute();
return result.getModifiedCount();
}