2016-11-17 6 views
2

대량 업데이트는 1.9.0.RELEASE의 spring-data-mongodb에서 지원됩니다.스프링 데이터 몽고 대량 업데이트

BulkOperations ops = template.bulkOps(BulkMode.UNORDERED, Match.class); 
for (User user : users) { 
    Update update = new Update(); 
    ... 
    ops.updateOne(query(where("id").is(user.getId())), update); 
} 
ops.execute(); 

mongoTemplate에는 void save (Object objectToSave)라는 함수가 있습니다. 전체 레코드를 삽입하거나 업데이트하려고하지만 일부 특정 필드는 업데이트하지 않습니다. Update 클래스를 무효화 할 수있는 방법이나 기능이 있습니까?

어쩌면 이렇게 될 수 있습니다 ..?

BulkOperations ops = template.bulkOps(BulkMode.UNORDERED, Match.class); 
for (User user : users) { 
    ... 
    ops.save(query(where("id").is(user.getId())), user); 
} 
ops.execute(); 

답변

0

삭제 및 삽입은 선택할 수 있지만 실패 할 경우이 옵션을 선택하기 전에 데이터를 복원해야합니다.

2

이 보인다 그 upsert는 봄 데이터 MongoDB를 대량 작업에서 지원되지 않습니다 (쿼리 쿼리, 개체 개체).

우리가에서 업데이트 객체를 생성하는 Update.fromDBObject 방법을 사용할 수 있습니다 그러나, DBOBJECT :

BulkOperations bulkOps = mongoOperations.bulkOps(BulkOperations.BulkMode.ORDERED, entityInformation.getJavaType()); 

    // add "save" operation for each entity 
    MongoConverter converter = mongoOperations.getConverter(); 
    ConversionService conversionService = converter.getConversionService(); 
    com.mongodb.DBObject dbObject; 
    for (S entity : entities) { 
     if (entityInformation.isNew(entity)) { // --- if NEW entity, then generate id and INSERT --- 

      // generate NEW id 
      ID id = conversionService.convert(new ObjectId(), entityInformation.getIdType());     
      entity.setId(id); 
      // insert 
      bulkOps.insert(entity); 

     } else { // --- if EXISTING entity, then UPSERT --- 

      // convert entity to mongo DBObject 
      dbObject = new BasicDBObject(); 
      // NULL fields will NOT BE UPDATED - will be ignored when converting an entity to a {@link com.mongodb.DBObject} 
      // and thus they will not be added to the {@link Update} statement. 
      converter.write(entity, dbObject);     
      // upsert 
      bulkOps.upsert(new Query(Criteria.where(UNDERSCORE_ID).is(dbObject.get(UNDERSCORE_ID))), 
          Update.fromDBObject(new BasicDBObject("$set", dbObject))); 
     } 
    } 
    // execute bulk operations 
    bulkOps.execute(); 
0

이 작업을 시도 할 수 있습니다 :

BulkOperations ops = mongoOps.bulkOps(BulkMode.<ordered/unordered>,<your ob>.class); 
loop on your batch { 
    Update update = Update.fromDBObject(
     BasicDBObjectBuilder.start("$set", <your ob>).get() 
    ); 
    ops.upsert(query(where("").is(<your ob>.something())), update); 
} 
ops.execute(); 

이 전체를 업데이트합니다 pojo (일부 특정 필드가 아님).

관련 문제