I am using spring-boot-starter-data-mongodb-reactive at the current latest version. I need to update a count field in many documents in my collection, but the count field is different for each document. I am looking for a way to perform a bulk update so that I do not have to perform an update for each item, separately, a million times.
My first approach has been to create a list of UpdateOneModel containing the Criteria and the Update. I can get the collection from the ReactiveMongoOperations instance, but this feels like quite an awkward way to do it. It looks like this:
Mono<MongoCollection<Document>> collection = mongoOps.getCollection(mongoOps.getCollectionName(Foo.class));
BulkWriteOptions options = new BulkWriteOptions()
.bypassDocumentValidation(true)
.ordered(false);
return result.getCounts()
.reduce(<Creating a map of ID to new count>)
.map(<Creating an UpdateModel<Document> instance)
.map(updates -> collection.map(c -> c.bulkWrite(updates, bulkWriteOptions)))
.then();
This feels like an odd (and almost brute-force) way to try to perform a bulk update. Am I missing something? Spring usually includes methods for performing bulk updates, but their reactive mongo library does not apparently include it. What else might I try?