首页 > 解决方案 > Mongo:锁定单个文档以供 READ 和后续更新

问题描述

我有一个存储文档的 Mongo Collection。每个文档都有一个字段complexNestedObject,顾名思义,它包含一个复杂的嵌套对象。我必须创建一个实现以下逻辑的函数

updateComplexNestedObject(_id, data) {
  1) read the document using _id as key
  2) perform some logic based on input data and the content of the document
  3) based on this logic determine the new value of complexNestedObject
  4) update complexNestedObject with the new value
}

这个函数可以被不同的客户端同时调用,对于同一个文档_id但输入数据不同,所以我需要实现一些锁定机制。哪种锁定机制最好?我应该使用 Mongo 4 事务支持还是更好的一些应用解决方案?

标签: mongodb

解决方案


推荐阅读