Spring mongodb add or update a list of documents

BiJ picture BiJ · Apr 8, 2017 · Viewed 11k times · Source

I have a list of records like

[
    {"id":"1", "name":"a", "user":"u1"},
    {"id":"2", "name":"b", "user":"u1"},
    {"id":"3", "name":"c", "user":"u1"}
]

Now based on if an entry already exists or not in the database, it should either update or insert the document. Also for update there is a condition that the value of existing user field should match the supplied value for user in the document.

Of course I can run the list in a loop and use

mongoOperations.save(...);

But if I have a huge list then I will have to do one db operation per each entry which I don't think is efficient. Is there any other efficient way to perform this operation?

Answer

Narendra Jaggi picture Narendra Jaggi · Apr 10, 2017

If you are using the CRUD repository then the CRUD repository provides the save() method which can be used for the single entity(mongoCollection) or you can use the overloaded save method

<S extends T> List<S> saveAll(Iterable<S> entites)

which can take the Arraylist and saves arraylist object. No need to use the loops.

You can see the below example in which InventoryService class create a 3 Inventory Objects and add all in ArrayList and finally pass this to inventory repository which is a CRUD repository.

@Service
public class InventoryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(InventoryService.class);

    @Autowired
    private InventoryRepository inventoryRepository;

    public void aveInventoryDetails() {

        List<Inventory> inventoryList = new ArrayList<Inventory>();

        inventoryList.add(new Inventory("500", 10));
        inventoryList.add(new Inventory("600", 20));
        inventoryList.add(new Inventory("700", 30));

        try {
            inventoryRepository.saveAll(inventoryList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Sample Mongo Repository

package com.bjs.repository;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.repository.MongoRepository;

import com.bjs.model.Inventory;

public interface InventoryRepository extends MongoRepository<Inventory, String> {

// empty as not defining any new method , we can use the existing save method   

}

For reference - http://docs.spring.io/autorepo/docs/spring-data-commons/1.9.1.RELEASE/api/org/springframework/data/repository/CrudRepository.html#save-java.lang.Iterable-