作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面是我的示例文档。我想加载此文档并读取total_comments 值并对其执行某些逻辑,并使用新的total_comments 值再次更新文档。在完成上述步骤之前,我应该如何确保 Total_comments 值不会被其他请求更新?
{ "_id" : ObjectId("56782a933d5c6ca02100002b"), "total_comments" : 12, "time_updated" : 1450715963 }
在mysql中,我们可以通过“select for update”来做到这一点?我们如何在 Mongodb 中实现这一点?
这是我的 MongoDB 版本:
> db.version()
3.0.7
这是我的存储引擎详细信息:
> db.serverStatus().storageEngine
{ "name" : "mmapv1" }
最佳答案
使用findAndModify()更新文档。
例如,您可以将total_comments增加1
db.collection.findAndModify({
_id: ObjectId("56782a933d5c6ca02100002b")
}, {}, {
$inc: { "total_comments": 1 }
});
关于mongodb - 我们如何在 Mongodb 中实现 "Select For Update"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34421000/
我是一名优秀的程序员,十分优秀!