Mongoose version: when can I turn it off?

From docs :

VersionKey is the property set for each document when it was first created by Mongoose. The value of this key contains an internal revision of the document. The name of this document property is customizable. The default value is __v. If this contradicts your application, you can configure as such:

[...]

Versioning of documents can also be disabled by setting the version of Key to false. DO NOT disable version control if you do not know what you are doing.

But I'm curious in which cases it would be safe to disable this feature?

+8
mongodb mongoose
source share
1 answer

The purpose of the version key is optimistic locking.

When enabled, the version value increases atomically each time the document is updated.

This allows you to check your application code if changes were made between the selection (for example, using the version 42 key) and the subsequent update (while the version value is still 42). If the version key has a different meaning (for example, 43 because the update was done in the document), your application code can handle concurrent modification.

The concept itself is often used in relational databases instead of pessimistic locking, which can bring terrible performance. A similar function provides all ORM descent. For example, this is well described in the ObjectDB documentation . This is an object database implemented in Java, but the same concept applies.

Message in connection with the comment of the question. I found the explanation on the mongoose-orm mailing list pretty clear: if you need optimistic locking for other fields, you need to handle it yourself.

Here is a gist that shows how to implement a retry strategy for the add operation. Again, how you want to handle this depends on your use cases, but that should be enough to get you started.

Hope this clarifies the situation.

Greetings

+20
source share

All Articles