免责声明:基于GitHub上的这个问题,我自己编写了插件以在我自己的代码中解决这个问题。
这已经很晚了,但希望总比没有好。
该猫鼬更新-如果电流插件可能给你你正在寻找的功能。它.save()使用版本字段在调用文档时添加了乐观并发控制。__v每次保存文档时它都会增加,并防止不同的版本相互保存。例如:
// saves with __v = 0
let product = await new Product({ name: 'apple pie' }).save();
// query a copy of the document for later (__v = 0)
let oldProduct = await Product.findById(product._id);
// increments to __v = 1
product.name = 'mince pie';
product = await product.save();
// throws an error due to __v not matching the DB version
oldProduct.name = 'blueberry pie';
oldProduct = await oldProduct.save();
Run Code Online (Sandbox Code Playgroud)
因此,我们现在对Product.
警告:该插件.save()仅验证版本,而不是在静态模型方法(如Product.findByIdAndUpdate().
| 归档时间: |
|
| 查看次数: |
597 次 |
| 最近记录: |