2016-11-17 7 views
2

バルクアップデートは1.9.0.RELEASEのspring-data-mongodbからサポートされています。スプリングデータmongoバルクアップデート

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

それは春データのMongoDB一括操作でサポートされていませんアップサート(クエリ、クエリ、Objectオブジェクト)ようです。

我々はから更新オブジェクトを生成する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(いくつかの特定のフィールドではありません)。

関連する問題