2

https://github.com/spring-projects/spring-data-mongodb/issues/2821

https://jira.spring.io/browse/DATAMONGO-1922?redirect=false

I have been looking for ReactiveBulk operations to update documents as a batch in Spring WebFlux.

Like in the Mongo Template

var bulkOps = mongoTemplate.bulkOps()
for(dto : List<DTO> DTOs) {
  Query query = new Query();
  query.addCriteria(Criteria.where(ID).is(dto.getId()));
  Update update = new Update()
                    .set(STATUS, dto.getStatus())
  bulkOps.updateOne(query, update)
}
bulkOps.execute();

is there a workaround to implement that operation in reactive way since reactivemongotemplate look like does not support that operation currently?

similiar topic in so: Bulk Update with ReactiveMongoTemplate

1 Answer 1

2

Quickly remind that Bulk is different than UpdateMulti.

Bulk is meant to write multiple objects in a single query, therefore, update various objects. On the other side, UpdateMulti is intended to update all lines where expression matches

As for reactive bulk, you should be able to use ReactiveMongoTemplate and implement something like that:

reactiveMongoTemplate.getCollection("collection_name")
    .flatMap(mongoCollection -> {
            List<UpdateOneModel<Document>> operations = DTOs.stream()
                .map(dto -> {
                    Document doc = new Document("status", dto.getStatus());
                    
                    reactiveMongoTemplate.getConverter().write(dto, doc);
                    
                    Document filter = new Document("id", dto.getId());
                    
                    return new UpdateOneModel<Document>(filter, new Document("$set", doc))
                }).toList();
        
            return Mono.from(mongoCollection.bulkWrite(operations));
    });

You can also add custom options to bulkWrite() if you desire.

If more filter is needed, you can append them to the document

Document filter = new Document("id", dto.getId())
     .append("country", dto.getCountry);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.