Skip to content

Add support for reactive bulk operations [DATAMONGO-1922] #2821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Apr 9, 2018 · 3 comments
Closed
Assignees
Labels
in: core Issues in core support type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

Mark Paluch opened DATAMONGO-1922 and commented


1 votes, 2 watchers

@tgrushka
Copy link

tgrushka commented May 7, 2022

Bulk upsert would be wonderful, as insertAll fails with a DuplicateKeyException if even one record is duplicated, and this exception cannot seem to be caught and ignored, such as in the following code:

private fun <T: DataModel> insertAllValid(uri: String, transformer: (JSONObject) -> InputModel<T>) {
    getJSON(uri).map(transformer)
        .map(InputModel<T>::toDataModelIfValid)
        .mapNotNull { it!! }
        .collectList()
        // Would be nice to have `::upsertAll` that bulk inserts new + updates existing records
        .flatMapMany(mongoTemplate::insertAll)
        .onErrorResume(DuplicateKeyException::class.java) { e ->
            println(e)
            Flux.empty()
        }
        .doOnNext { logger.debug(it.toString()) }
        .doOnError { logger.error(it.toString()) }
        .subscribe()
}

@almogtavor
Copy link

@tgrushka A nice workaround:

reactiveMongoTemplate.getCollection(config.getCollection()).flatMap(mongoCollection -> {
        var operations = entities.stream().map(entity -> {
            Document doc = new Document();
            reactiveMongoTemplate.getConverter().write(entity, doc);
            var filter = new Document("externalId", entity.getExternalId());
            return new UpdateOneModel<Document>(filter, new Document("$set", doc), new UpdateOptions().upsert(true));
        }).toList();
        return Mono.from(mongoCollection.bulkWrite(operations));
    })

@argmnt
Copy link

argmnt commented May 20, 2022

@almogtavor Instead of upsert how can I update like this way
var bulkOps = mongoTemplate.bulkOps()
for(dto : List 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();

mp911de added a commit that referenced this issue Apr 14, 2023
Extract common code into BulkOperationsSupport. Reorder methods. Add missing verifyComplete to tests.

See #2821
Original pull request: #4342
@mp911de mp911de added this to the 4.1 RC1 (2023.0.0) milestone Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core support type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants