数据库中有这样一个 document:
{
_id: 1,
name: "sue",
badges: [ "blue", "black" ],
points: [
{ points: 85, bonus: 20 },
{ points: 85, bonus: 10 }
]
}
两个用户 A/B,同时向 points 插入一个新数据, 譬如 A 插入 {points: 90, bonus: 20}, B 用户插入{ points: 95, bonus: 10 } 如何才能保障一致性?
findAndModify 能搞定吗?还是一点要加锁?
1
lianyue 2018-10-13 13:08:06 +08:00 1
|
2
dupreesun OP 非常感谢!
|
3
menyakun 2018-10-13 14:25:28 +08:00
https://docs.mongodb.com/manual/faq/concurrency/index.html#what-locks-are-taken-by-some-common-client-operations
按文挡的解释,findOneAndModify 应该也是可以的,这个操作 mongodb 会自动加 Document 级别的锁 |