【发布时间】: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