MongoDB数据完整性 [英] MongoDB data integrity

查看:100
本文介绍了MongoDB数据完整性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MongoDB的新手,最难理解的是如何确保数据完整性.

I'm new to MongoDB, and the most difficult to understand is how to ensure data integrity.

我有两个收藏集->评论(一对多).

I've got two collections Post -> Comment (One to many).

是否有一种方法可以存储每个帖子的评论数量,而无需使用

Is there a way to store number of comments for each post, without of using two phase commit?

Post {
    _id,
    text,
    commentsNumber
}

Comment {
    _id,
    text,
    postId
}

添加/删除注释时,必须增加/减少commentsNumber.这是对两个不同集合的两个请求.例如在MongoDB中,写操作在单个文档的级别上是原子的,可以添加/删除注释,但不能更新commentNumber,反之亦然.

When comment is added / removed, commentsNumber have to be incremented / decremented. And this is two requests to two different collections. Such as in MongoDB a write operation is atomic on the level of a single document, there is a chance that comment will be added / removed, but commentsNumber won't be updated or vise versa.

保证完整性的技术有哪些?

What are the techniques to guarantee integrity?

  • 是否每隔一段时间运行脚本以更新commentsNumber?
  • 根本不存储commentsNumber吗?
  • Run script every period of time to update commentsNumber?
  • Not to store commentsNumber at all?

推荐答案

除了所提到的两阶段提交之外,我怀疑是否有任何东西可以保证数据完整性.至少直到宣布v4 .

I doubt there is anything that can guarantee data integrity apart from the mentioned 2-phase commit. At least until announced v4.

几乎没有什么东西可以最大程度地减少错误计数的机会. 将插入和更新合并为一个批量.由于这是单个请求,因此将减少操作之一在应用程序侧失败的机会.

There are few things to minimise chances of get wrong counts. Combine insert and update into a single bulk. It will reduce chances that one of the operations fails on application side, since it is a single request.

然后检查是否nInserted === 1nModified === 1.否则,请为给定的职位ID重试或加入重新计算作业.

Then check if nInserted === 1, and nModified === 1. Otherwise re-try or enqueue a re-calculation job for the given post id.

要重试,必须具有可重试写入已启用,因为您将在帖子上使用$inc,这与幂等运算相距很远.

For re-tries it is essential to have retryable writes enabled, as you are going to use $inc on posts, which is quite far from idempotent operation.

另一种选择是应用无交易方法-一种运行脚本"的组合每隔一段时间即可更新commentsNumber"和完全不存储commentNumber".您将需要保留上一次重新计算作业的时间戳,并计算自该日期以来的新注释.

Another option is to apply transactionless approach - a kind of a combination of "Run script every period of time to update commentsNumber" and "Not to store commentsNumber at all". You will need to keep timestamps of the last re-calculation job, and count new comments since the date.

这篇关于MongoDB数据完整性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆