【问题标题】:Synchronized update of documents using Mongoose使用 Mongoose 同步更新文档
【发布时间】:2025-12-22 09:10:10
【问题描述】:

我在 Kubernetes 集群中部署了一个 nodejs API 服务器。 用户可以对拍卖品进行投标。 为了防止一个出价覆盖另一个出价,需要进行一些同步。

我在收到的出价中看到以下内容:

  • 启动一个事务,读取当前出价并将其与传入的出价进行比较并更新记录
  • 创建与上述相同的聚合

我不知道该走哪条路。我还了解到您需要使用 IX 或 X 锁定文档。

对于 RDBMS,您将创建一个事务来锁定记录并在更新后释放它,但我不知道它对 MongoDB 是如何工作的。

Product.findById(productId)
  .then(productmatch => {
    if (productmatch.currentPrice > price) throw Error('no go')

    const bid = new Bid({
      price,
      date: Date.now(),
      user: user._id
    })
    return Product.findByIdAndUpdate(productId, {
        $push: {
          bids: bid
        },
        currentPrice: price,
        currentUser: user,
        currentBid: bid
      }, {
        new: true
      })
      .then(product => {
        if (!product) throw Error(`no go`)

        return bid._id.toString()
      })
  })

【问题讨论】:

    标签: node.js mongodb mongoose concurrency


    【解决方案1】:

    经过更多研究,我想出了这个解决方案,但是我不知道它是否 100% 可靠,但我相信这种方法会锁定文档,并且不会让任何其他线程在查询和更新操作之间读取文档。

    var query = {
        _id: productId,
        closed: false,
        currentPrice: {
          $lt: price
        }
      },
      update = {
        $push: {
          bids: bid
        },
        currentPrice: price,
        currentUser: user,
        currentBid: bid
      },
      options = {
        new: true
      };
    return Product.findOneAndUpdate(query, update, options)
      .then(product => {
        if (!product) throw Error(`no product found with id ${productId}`)
        return bid._id.toString()
      })

    【讨论】:

      最近更新 更多