Fix model version not being checked properly on queries with push operands. Issue #1898 #1942

wants to merge 2 commits into


None yet
6 participants

This bug affects anyone who is depending on mongoose for optimistic locking and has arrays in their documents.


aheckmann commented Feb 28, 2014

Thanks for contributing. Please add a test demonstrating it fixes the issue.

Thanks Aaron. In investigating this further, I realize things are behaving more unexpected than I initially thought. The basic use case for document versioning seems to be broken as demonstrated in this test case:


I think fundamentally, the way this feature currently works is flawed. Nearly all modifications to a document should trigger the __v to be checked and incremented. It currently tries to do some optimizations that result in behavior that one would not expect when relying on this. If the user wants to modify a field without checking to see if they have the most recent copy of the document, it should be done so explicitly. Otherwise the default behavior should be to return a VersionError whenever the version does not match - even if it's an atomic operation being done like a $push or $inc.

fiznool commented May 1, 2014

Coming from a Hibernate world, I also expected document versioning to operate across the entire document, not just the subdocument array. The use case being that if two users are modifying the same document, without document versioning we have a 'last user wins' scenario where the saves made by the first user will get overwritten by the second user.

@aheckmann is it possible for you to describe why versioning only operates at the subdocument level rather than on the document as a whole?

@fiznool @aheckmann Did you find a solution to get mongoose to set versions for the main document? (rails is the same way)

fiznool commented May 26, 2014

@ryanstout I haven't explored this in any more detail. I'm waiting to hear if there is a reason why it isn't versioning on the entire document.

BTW, the work around I found for this is to call increment() on the document manually in our application, which will ensure the version check is done for optimistic locking.

paglias commented Jul 15, 2016

@vkarpov15 do you know if this is still happening? We've an issue that might be caused by the versioning key not being checked/increased correctly when saving a document with a plain array modified by using doc.array.push().


vkarpov15 commented Jul 16, 2016

@paglias can you open up a separate issue? This PR is super stale.

vkarpov15 closed this Jul 16, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment