当前位置: 代码迷 >> 综合 >> java spring data mongo 批量修改
  详细解决方案

java spring data mongo 批量修改

热度:76   发布时间:2023-10-17 15:57:58.0

一千条数据修改一千次很慢

一千条数据只修改一次很快

    @Testpublic void test() {List<Tasks> taskList = mongoTemplate.findAll(Tasks.class, ArtCommonConstant.CollectionConfig.TASK_COLLECTION);List<Template> templateList = mongoTemplate.findAll(Template.class, ArtCommonConstant.CollectionConfig.TEMPLATE_COLLECTION);//List<BathUpdateOptions>HashMap<String, Integer> map = new HashMap<String, Integer>(32);for (Template template : templateList) {map.put(template.getId(), template.getMarkType());}List<WriteModel<Document>> requests = new ArrayList<WriteModel<Document>>();  //创建参数集合'for (Tasks task : taskList) {Document queryDocument = new Document("_id", task.getId());  //条件Integer markType = map.get(task.getTempId());Document updateDocument = new Document("$set", new Document("markType", markType));  //更改信息UpdateOneModel<Document> uom = new UpdateOneModel<Document>(queryDocument, updateDocument, new UpdateOptions().upsert(false));requests.add(uom);}BulkWriteResult bulkWriteResult = mongoTemplate.getCollection(ArtCommonConstant.CollectionConfig.TASK_COLLECTION).bulkWrite(requests);System.out.println("bulkWriteResult" + GsonJsonUtil.objectToJsonString(bulkWriteResult));}

  相关解决方案