使用Mongoose在MongoDB中处理查找,修改和保存流中的冲突 [英] Handling conflict in find, modify, save flow in MongoDB with Mongoose

查看:115
本文介绍了使用Mongoose在MongoDB中处理查找,修改和保存流中的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新一个涉及读取其他集合和复杂修改的文档,因此findAndModify()中的更新操作符无法达到我的目的.

I would like to update a document that involves reading other collection and complex modifications, so the update operators in findAndModify() cannot serve my purpose.

这就是我所拥有的:

Collection.findById(id, function (err, doc) {

    // read from other collection, validation
    // modify fields in doc according to user input
    // (with decent amount of logic)

    doc.save(function (err, doc) {
      if (err) {
        return res.json(500, { message: err });
      }

      return res.json(200, doc);
    });
}

我担心的是,如果多个客户端碰巧修改同一文档,则此流程可能会导致冲突.
据说此处:

My worry is that this flow might cause conflict if multiple clients happens to modify the same document.
It is said here that:

单个文档的操作对于MongoDB数据库始终是原子的

Operations on a single document are always atomic with MongoDB databases

我对Operations的含义有些困惑.

  • 这是否意味着findById()将获取锁,直到doc超出范围(发送响应后),这样才不会发生冲突? (我不这么认为)
  • 如果没有,那么如何修改我的代码以支持多个客户端,因为他们知道他们将修改Collection?
  • 猫鼬会在发生冲突时举报冲突吗?
  • 如何处理可能的冲突?是否可以手动锁定收藏集?
    • 我看到建议使用Mongoose的versionKey(或时间戳记)并重试过时的文档
    • 不要完全使用MongoDB ...
    • Does this means that the findById() will acquire the lock until doc is out of scope (after the response is sent), so there wouldn't be conflicts? (I don't think so)
    • If not, how to modify my code to support multiple clients knowing that they will modify Collection?
    • Will Mongoose report conflict if it occurs?
    • How to handle the possible conflict? Is it possible to manually lock the Collection?
      • I see suggestion to use Mongoose's versionKey (or timestamp) and retry for stale document
      • Don't use MongoDB altogether...

      谢谢.

      编辑

      感谢@jibsales作为指针,我现在使用Mongoose的versionKey(时间戳也将起作用)以避免提交冲突.

      Thanks @jibsales for the pointer, I now use Mongoose's versionKey (timestamp will also work) to avoid committing conflicts.

      aaronheckmann —猫鼬v3第1部分::版本控制

      请参阅以下示例代码:
      https://gist.github.com/anonymous/9dc837b1ef2831c97fe8

      See this sample code:
      https://gist.github.com/anonymous/9dc837b1ef2831c97fe8

      推荐答案

      操作是指读/写.切记MongoDB不是ACID兼容的数据层,如果您需要真正的ACID兼容,最好选择另一种技术.也就是说,您可以通过两阶段提交"技术实现原子性和隔离性,概述在本文的MongoDB文档中.这是一项艰巨的任务,因此请做好一些繁重的准备,因为您需要使用本地驱动程序而不是Mongoose.再次,我的最终建议是,如果您需要听起来像您一样的事务支持,则不要喝NoSQL koolaid.

      Operations refers to reads/writes. Bare in mind that MongoDB is not an ACID compliant data layer and if you need true ACID compliance, you're better off picking another tech. That said, you can achieve atomicity and isolation via the Two Phase Commit technique outlined in this article in the MongoDB docs. This is no small undertaking, so be prepared for some heavy lifting as you'll need to work with the native driver instead of Mongoose. Again, my ultimate suggestion is to not drink the NoSQL koolaid if you need transaction support which it sounds like you do.

      这篇关于使用Mongoose在MongoDB中处理查找,修改和保存流中的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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