大佬教程收集整理的这篇文章主要介绍了node.js – 具有版本号的Mongoose乐观并发,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这已经很晚了,但希望总比没有好.
mongoose-update-if-current插件可能会为您提供所需的功能.它使用版本字段在文档上调用.save()时添加了乐观并发控制.每次保存文档时它都会递增__v,并防止不@R_756_11197@相互保存.例如:
// 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();
因此,我们现在对Product有乐观的并发控制.
警告:该插件仅验证.save()中的版本,而不验证Product.findByIdAndupdate()等静态模型方法中的版本.
以上是大佬教程为你收集整理的node.js – 具有版本号的Mongoose乐观并发全部内容,希望文章能够帮你解决node.js – 具有版本号的Mongoose乐观并发所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。