spring-projects / spring-projects/spring-data-mongodb

Improve `MongoRepository.saveAll` to use batch updates

Open
#5,220 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

type: enhancement
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

MongoDB supports efficient batch operations through bulkWrite, and SimpleMongoRepository.saveAll(...) already performs a batch insert when all entities are new .

However, if entities contains any non-new entity, it calls SimpleMongoRepository.save(...) for each entity, which seems neither efficient nor consistent.

A possible improvement would be something like the following:

List<S> newEntities = new ArrayList<>();
List<S> existingEntities = new ArrayList<>();

// ...

if (!newEntities.isEmpty()) {
   mongoOperations.insert(newEntities, entityInformation.getCollectionName());
}

if (!existingEntities.isEmpty()) {

   BulkOperations bulkOps = mongoOperations.bulkOps(BulkMode.ORDERED, entityInformation.getJavaType(),
            entityInformation.getCollectionName());

   for (S entity : existingEntities) {
      Query query = new Query(where(entityInformation.getIdAttribute()).is(entityInformation.getId(entity)));
      bulkOps.replaceOne(query, entity, FindAndReplaceOptions.options().upsert());
   }

   bulkOps.execute();
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in SimpleMongoRepository.saveAll(...) and compare its all-new batch insert path with the per-entity save path for mixed collections. Read the MongoOperations bulk-operation entry points shown in the issue, then verify that new entities remain batch inserted and existing entities are replaced through one bulk operation with the intended upsert behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb
Domain
databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.