// list为要更新或者新增的数据,使用时填充上自己的数据集合
List<MultiAnalyseMakeModel> list = new ArrayList();

List<Pair<Query, Update>> updateList = new ArrayList<>(list.size());
        BulkOperations operations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, collection);// collection为集合名称
        list.forEach(data -> {
            Query query = new Query(new Criteria("_id").is(data.getId()));
            Update update;
            Document doc = new Document();
            mongoTemplate.getConverter().write(data, doc);
            update = Update.fromDocument(new Document("$set", doc));
            Pair<Query, Update> updatePair = Pair.of(query, update);
            updateList.add(updatePair);
        });
        operations.upsert(updateList);

        BulkWriteResult result = operations.execute();

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐