5

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?

BiJ
  • 1,639
  • 5
  • 24
  • 55

3 Answers3

2

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-

Abdur Rahman
  • 1,420
  • 1
  • 21
  • 32
Narendra Jaggi
  • 1,297
  • 11
  • 33
  • 1
    Or implement the loop by your own - `CrudRepository`'s implementation uses loop. – ZZ 5 Jan 23 '19 at 12:52
  • Is it possible to store -- List mappedResults = aggregate.getMappedResults(); List by saveAll – Bug Mar 07 '22 at 11:19
2

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);
letsDoThis
  • 71
  • 1
  • 7
0

You can just use:

mongoTemplate.insertAll(entities);
Tyler2P
  • 2,324
  • 26
  • 22
  • 31