void batchUpset(List<Entity> list) {
//UNORDERED: 并行,遇到错误后继续执行
BulkOperations bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, "entity_table");
//批量更新
for (Entity e: list) {
Query query = Query.query(Criteria.where("name").is(e.getName()));
Update update = new Update().set("score", e.getScore());
bulkOps.upsert(query,update);
}
bulkOps.execute();
//批量写入
bulkOps.insert(list);
bulkOps.execute();
//批量删除
for (Entity e: list) {
Query query = Query.query(Criteria.where("name").is(e.getName()));
bulkOps.remove(query);
}
bulkOps.execute();
}
|