Mongo一对多关系和更新问题

发布于 2024-09-11 19:56:40 字数 482 浏览 2 评论 0原文

我正在基于 MongoDB 实现博客。

首先让我们看看 Mongo 人如何建议我们存储博客文章及其评论(http:// /www.mongodb.org/display/DOCS/Schema+Design):

  • 帖子应该是一个集合。为了提高性能,评论应该嵌入帖子中的对象。

我发现在这个模式中编辑或批准评论非常困难。

由于评论是帖子文档的一部分,我无法单独编辑它,因为它没有标识符可以在评论集合中找到它。

这就是为什么我要立即编辑所有评论。另一个问题是,当我立即编辑所有评论时,有人可以发布新评论,并且在保存评论集合后它将被覆盖。

也许我做错了什么?或者 Mongo 人员描述的用例希望评论不会被编辑。

问候, 阿列克谢·扎哈罗夫。

I'm implementing blog based on MongoDB.

Let's look at first how Mongo guys recommend us to store blog post and its comments (http://www.mongodb.org/display/DOCS/Schema+Design):

  • posts should be a collection. comments should be embedded objects within a post for performance.

I found that it is very difficult to edit or just approve comments in this schema.

Due to comment is a part of Post document i cannot edit it separately because it has no identifier to find it in comments collection.

That is why I'm editing all comments at once. Another problem is that while i'm editing all comments at once someone could post a new comment and it will overrided after saving of comments collection.

May be I'm doing something wrong? Or the use case described by Mongo guys expect that comments won't be edited.

Regards,
Alexey Zakharov.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海螺姑娘 2024-09-18 19:56:40

您应该能够使用 原子运算符 来绕过“保存所有评论” ' 问题。例如,类似这样的内容应该有效:

db.posts.update({ _id: [ID] }, { $set: { "comments.5.body" : "New Comment Body" } })

编辑:只是为了扩展我上面的答案。原子运算符的工作方式是仅更新您告诉它们的特定字段,而不是整个文档。这不仅使您的更新更加简洁,而且速度也相当快。特别是,在您的示例中,您有一篇包含许多评论的大型博客文章。

You should be able to use Atomic Operators to get around the 'saving all comments' issue. For example, something like this should work:

db.posts.update({ _id: [ID] }, { $set: { "comments.5.body" : "New Comment Body" } })

Edit: Just to expand on my above answer. The way atomic operators work is by only updating the specific field(s) you tell them to, rather than the whole document. This not only makes your update more concise but also considerably faster. Especially if, in your example, you have a large blog post with many comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文