spring-data-mongodb
spring-data-mongodb copied to clipboard
Add support for MongoDB backed counters.
The goal of this issue is to help users manage server side counters more easily.
We could leverage findAndUpdate that issues an update with $inc, upsert and ReturnDocument.AFTER to maintain a counter document in a specific collection obtaining the incremented value as outlined below.
return mongoTemplate.execute("sequence_collection", collection -> {
return collection.findOneAndUpdate(new Document("_id", sequenceName), new Document("$inc", new Document("count", 1)), new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)).getLong("count");
});
Wrapping the above in something like a MongoSequence would allow easy access.
This issue is somehow related to #1842 which IMO should be addressed on a broader basis in data-commons.