Reputation: 13632
I understand that versioning in Mongoose is primarily intended to avoid issues with sub-document arrays. However, is it also possible to make use of the functionality to avoid conflicting updates to documents? i.e. two users updating the same document at the same time.
Ideally what I would like to do is something like this:
If I put a setTimeout() between 1 and 2, then manually update the __v directly before 3, the save still succeeds. I assume this is because Mongoose does not think it is necessary to include the version clause in the update in this case.
I've also tried calling increment() before the save(). I assumed this would increment the version during the save operation, however it seems as if it is incrementing the version, then upon save complaining it doesn't match what's in the DB (which it obviously wouldn't).
Am I better off just implementing versioning myself using middleware?
Upvotes: 1
Views: 1056
Reputation: 3696
You may want too look at MongoDBs internal findAndModify command.
http://www.mongodb.org/display/DOCS/Atomic+Operations#AtomicOperations-FindandModify%28orRemove%29
It is designed to do the process you have listed
Upvotes: 1