Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data MongoDB and Bulk Update

I am using Spring Data MongoDB and would like to perform a Bulk Update just like the one described here: http://docs.mongodb.org/manual/reference/method/Bulk.find.update/#Bulk.find.update

When using regular driver it looks like this:

The following example initializes a Bulk() operations builder for the items collection, and adds various multi update operations to the list of operations.

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "D" } ).update( { $set: { status: "I", points: "0" } } );
bulk.find( { item: null } ).update( { $set: { item: "TBD" } } );
bulk.execute()

Is there any way to achieve similar result with Spring Data MongoDB ?

like image 640
Rafal G. Avatar asked Oct 30 '14 15:10

Rafal G.


1 Answers

Bulk updates are supported from spring-data-mongodb 1.9.0.RELEASE. Here is a sample:

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();
like image 146
jalogar Avatar answered Oct 13 '22 07:10

jalogar