Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

294
Vistas
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?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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-

about 4 years ago · Santiago Trujillo Denunciar

0

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);
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda