Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Mongodb Bulk Operation Example

Can some one please point me a complete example of Spring Data Mongodb DB bulk operation example.

I am trying to switch to bulk updates using spring data mongodb. Not able to find a good example.

Thank you.

like image 224
msameep Avatar asked Sep 16 '19 20:09

msameep


People also ask

What is bulk in MongoDB?

Bulk() Bulk operations builder used to construct a list of write operations to perform in bulk for a single collection. To instantiate the builder, use either the db. collection. initializeOrderedBulkOp() or the db.

How MongoDB read data in spring boot?

Read operations using Spring Boot MongoDBFetch all the documents (grocery items) using the findAll() method. Get a single item (document) by its name field using the findItemByName method. Get a list of items based on a category.

Does spring support MongoDB?

The Spring Data MongoDB project provides integration with the MongoDB document database. Key functional areas of Spring Data MongoDB are a POJO centric model for interacting with a MongoDB DBCollection and easily writing a Repository style data access layer.

What is bulk operation in Java?

Posted by: Michael Scharhag in Enterprise Java May 10th, 2021 0 3264 Views. Bulk (or batch) operations are used to perform an action on more than one resource in single request. This can help reduce networking overhead.


2 Answers

BulkOprations in Spring data mongodb uses bulkWrite() from mongodb.
From mongoDB documentation ->

MOngoDbDocumention

So When you want to update many entities with different updated in one query you can do that via this bulkOps.

Let us see an example eventhough it may not be an perfect one. Lets consider you have an Employee Collection with employees working in a company. Now After appraisal there will be change in salary for all the employees, and each employee salary change will be different and let's pretend there is no percentage wise hike involved and if you want to update the changes in one go you can use bulkOps.

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.util.Pair;

public class Example {
    @Autowired
    MongoTemplate mongoTemplate;
    public int bulkUpdateEmployee(List<Pair<Query, Update>> updates){
        return mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED,"employees",Employee.class).updateMulti(updates).execute().getModifiedCount();
    }
}

--------------Here we can prepare the pair of query and update from ------- -------for each employee-> ---"query" - id of employee is blabla ---"update"- set salary to xxx

like image 183
Lucia Avatar answered Oct 10 '22 21:10

Lucia


Sharing the code for bulk operations which worked for me

BulkOperations bulkOps = mongoTemplate.bulkOps(BulkMode.UNORDERED, Person.class);
for(Person person : personList) {
    Query query = new Query().addCriteria(new Criteria("id").is(person.getId()));
    Update update = new Update().set("address", "new Address as per requirement");
    bulkOps.updateOne(query, update);
}
BulkWriteResult results = bulkOps.execute();
like image 42
abhinav kumar Avatar answered Oct 10 '22 23:10

abhinav kumar