Spring data mongo bulk update
Asked Answered
D

5

7

Bulk updates are supported from spring-data-mongodb from 1.9.0.RELEASE.

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 has the function called void save(Object objectToSave); I want to insert/update the whole record but not some particular fields. Is there any way or function that I can void the Update class?

Maybe something like this..?

BulkOperations ops = template.bulkOps(BulkMode.UNORDERED, Match.class);
for (User user : users) {
    ...
    ops.save(query(where("id").is(user.getId())), user);
}
ops.execute();
Disfranchise answered 17/11, 2016 at 3:48 Comment(0)
C
4

It seems that the upsert(Query query, Object object) it is not supported in the Spring Data MongoDB bulk operations.

However we can use Update.fromDBObject method to generate an Update object from a 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();
Covarrubias answered 1/3, 2017 at 6:18 Comment(0)
G
3

You can try this:

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();

This will update entire pojo(not some particular fields) just like save.

Garbanzo answered 21/8, 2017 at 8:24 Comment(0)
D
0

org.springframework.data.mongodb.core.MongoTemplate provides the implementation of BulkWriteOperation. You can use it.

BulkWriteOperation bulkWriteOperation= mongoTemplate.getCollection(collectionName).initializeUnorderedBulkOperation();
Danikadanila answered 10/7, 2019 at 7:16 Comment(0)
T
-1
    BulkOperations bulkOperations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, "someCollection");
    bulkOperations.insert(addressRanges);
    bulkOperations.execute();

This is all you need.

Tremulous answered 20/10, 2021 at 21:45 Comment(0)
S
-2

Delete and insert will be the option you can opt for but you need to take back up of your data before you choose this options in case of any failures.

Stretcherbearer answered 5/2, 2017 at 13:22 Comment(1)
Terrible idea. Very easy to end up in an inconsistent state.Aerator

© 2022 - 2024 — McMap. All rights reserved.