Mongoose 版本控制:什么时候禁用它是安全的? [英] Mongoose versioning: when is it safe to disable it?

查看:55
本文介绍了Mongoose 版本控制:什么时候禁用它是安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自文档:

versionKey 是每个文档第一次创建时设置的属性猫鼬.此键值包含内部版本文档.此文档属性的名称是可配置的.这默认为 __v.如果这与您的应用程序冲突,您可以配置如下:

The versionKey is a property set on each document when first created by Mongoose. This keys value contains the internal revision of the document. The name of this document property is configurable. The default is __v. If this conflicts with your application you can configure as such:

[...]

也可以通过将 versionKey 设置为禁用文档版本控制错误的.除非您知道自己在做什么,否则请勿禁用版本控制.

Document versioning can also be disabled by setting the versionKey to false. DO NOT disable versioning unless you know what you are doing.

但我很好奇,在哪些情况下禁用此功能应该是安全的?

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

推荐答案

版本键的目的是乐观锁.

The version key purpose is optimistic locking.

启用后,每当更新文档时,版本值都会自动递增.

When enabled, the version value is atomically incremented whenever a document is updated.

这允许您的应用程序代码测试在获取(例如引入版本密钥 42)和随后的更新(确保版本值仍然是 42)之间是否进行了更改.如果版本键具有不同的值(例如 43,因为对文档进行了更新),您的应用程序代码可以处理并发修改.

This allow your application code to test if changes have been made between a fetch (bringing in version key 42 for example) and a consequent update (ensuring version value still is 42). If version key has a different value (eg. 43 because an update has been made to the document), your application code can handle the concurrent modification.

在关系数据库中经常使用完全相同的概念,而不是会带来可怕的性能的悲观锁定.所有体面的 ORM 都提供这样的功能.例如,在 ObjectDB 文档 中对其进行了很好的描述.它是一个用 Java 实现的对象数据库,但适用相同的概念.

The very same concept is often used in relational databases instead of pessimistic locking that can bring horrible performance. All decent ORMs provide such a feature. For example it's nicely described in ObjectDB documentation. It's an object database implemented in Java but the same concept applies.

Behlül 评论中链接的 博客文章 展示了乐观锁用一个具体的例子,但只适用于数组的变化,见下文.

The blog post linked in Behlül's comment demonstrate the optimistic locking usefulness with a concrete example, but only for arrays changes, see below.

相反,这是一个无用的简单案例:可由其所有者自行编辑的用户配置文件.在这里,您可以摆脱乐观锁定并假设最后一次编辑总是获胜.

On the opposite, here is a simple case where its useless: a user profile that can be edited by its owner ownly. Here you can get rid of optimistic locking and assume that the last edit always wins.

因此,只有您知道您的应用程序是否需要乐观锁.逐个用例.

So, only you know if your application need optimistic locking or not. Use case by use case.

猫鼬的情况有些特殊.

仅对数组启用乐观锁定,因为内部存储格式使用位置索引.这是博客文章所描述的问题,链接在问题的评论.我找到了 解释mongoose-orm 邮件列表很清楚:如果你需要其他字段的乐观锁,你需要自己处理.

Optimistic locking is enabled only for arrays because the internal storage format uses positional index. This is the issue described by the blog post linked in the question's comment. I found the explanation given in the mongoose-orm mailing list pretty clear: if you need optimistic locking for other fields, you need to handle it yourself.

这是一个要点,展示了如何为add<实施重试策略/code> 操作.同样,您想如何处理它取决于您的用例,但它应该足以让您入门.

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

我希望这能解决问题.

干杯

这篇关于Mongoose 版本控制:什么时候禁用它是安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆