方法:mongoTemplate.getDb().getCollection(tableName).bulkWrite(requests);
public BulkWriteResult bulkWrite(List<WriteModel<Document>> requests,String tableName) {
BulkWriteResult write = mongoTemplate.getDb().getCollection(tableName).bulkWrite(requests);
return write;
}
BulkWriteResult write = mongoTemplate.getDb().getCollection(tableName).bulkWrite(requests);
难点在与如何拼接参数List<WriteModel<Document>> requests
例子:
DBObject queryObject = new BasicDBObject();
queryObject.put("doctorId",doctorId);
DBObject fields = new BasicDBObject();
fields.put("_id",1);
String tableName = Constant.hospitalInfo.getPrefixNo().toLowerCase()+"-record";
List<Map> map = mongoService.findByMapAndCriteriaMap(queryObject.toString(),fields.toString(),Map.class,
tableName);
List<WriteModel<Document>> requests = new ArrayList<WriteModel<Document>>(); //创建参数集合
for(int i=0;i<map.size();i++){
Document queryDocument = new Document("_id",map.get(i).get("_id")); //条件
Document updateDocument = new Document("$set",new Document(temp.getColName(),dataResult[i])); //更改信息
UpdateOneModel<Document> uom = new UpdateOneModel<Document>(queryDocument,updateDocument,new UpdateOptions().upsert(false));
requests.add(uom);
}
BulkWriteResult bulkWriteResult = mongoService.bulkWrite(requests,tableName);
queryObject.put("doctorId",doctorId);
DBObject fields = new BasicDBObject();
fields.put("_id",1);
String tableName = Constant.hospitalInfo.getPrefixNo().toLowerCase()+"-record";
List<Map> map = mongoService.findByMapAndCriteriaMap(queryObject.toString(),fields.toString(),Map.class,
tableName);
List<WriteModel<Document>> requests = new ArrayList<WriteModel<Document>>(); //创建参数集合
for(int i=0;i<map.size();i++){
Document queryDocument = new Document("_id",map.get(i).get("_id")); //条件
Document updateDocument = new Document("$set",new Document(temp.getColName(),dataResult[i])); //更改信息
UpdateOneModel<Document> uom = new UpdateOneModel<Document>(queryDocument,updateDocument,new UpdateOptions().upsert(false));
requests.add(uom);
}
BulkWriteResult bulkWriteResult = mongoService.bulkWrite(requests,tableName);