Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring mongodb add or update a list of documents

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?

like image 389
BiJ Avatar asked Apr 08 '17 20:04

BiJ


2 Answers

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-

like image 73
Narendra Jaggi Avatar answered Oct 16 '22 22:10

Narendra Jaggi


You can use mongoTemplate.updateMulti() for updating list of record as below

Query query = new Query(); 
query.addCriteria(Criteria.where("filteringField").is("filteringFieldValue));
Update update = new Update();
update.set("fieldToBeUpdated", "fieldToBeUpdatedValue");
mongoTemplate.updateMulti(query, update, YourClass.class);
like image 2
letsDoThis Avatar answered Oct 16 '22 20:10

letsDoThis