实际开发中多存在使用mongo批量存储数据list使用场景。但如果list中间某一条数据在存储出现异常如何避免对后边数据没有影响?
@Slf4j @Service public class PositionBuldRepository implements Serializable { @Autowired private MongoTemplate mongoTemplate; public void bulkInsert(List<Map> list, String className){ if(!CollectionUtils.isEmpty(list)) { BulkOperations ops = prepareBulkOperations(list, className); try { BulkWriteResult bulkWriteResult = ops.execute(); } catch (BulkOperationException e) { log.error("Some bulk operations failed.", e); } catch (RuntimeException e) { log.error("Unexpected exception while persisting positions.", e); } } } private BulkOperations prepareBulkOperations(List<Map> list,String className){ BulkOperations ops=mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED,className); list.forEach(ops::insert); return ops; } }