MongoDB 论坛模型
所以我正在为论坛创建一个模型。想想线程和一堆评论在哪里。主题有很多评论。在 RDBMS 世界中,我会这样设计。
Thread --has many--> Comment
id id
user_id thread_id
user_id
现在,我想,有了这个,数据/模式将遵循许多正常形式之一(我忘了是哪一种)。我认为这是最明智的做法。然而,当在 NoSQL 世界 (MongoDB) 中执行此操作时,设计这种关系的最佳方法是什么?我几乎可以用 RDBMS 方式做到这一点,但我会失去使用嵌入式对象的优势。出于某种原因,我更倾向于这样做,就像
Thread
_id
user_id
comments => [{_id, user_id, body, created_at}]
什么是最明智的方法一样,我想这就是我要问的。为什么?
So I am creating a model for a forum. Think thread and a bunch of comments where. Thread has many comments. In RDBMS world, I would design this as such
Thread --has many--> Comment
id id
user_id thread_id
user_id
Now, I guess, with this, the data/schema would follow one of the many normal forms (I forgot which). And I think this is the most sensical way to do it. However, when it comes to doing this in NoSQL world (MongoDB), what would be the best way to design this relationship? I could almost do it in the RDBMS way, but I would lose the advantage of using embedded object. For some reason, I am more inclined to do it like
Thread
_id
user_id
comments => [{_id, user_id, body, created_at}]
What would be the most sensical way to do this I guess that's what I am asking. And why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我希望您已经阅读了 Schema Design 的文档,其中解释了一个与你类似的例子。
因此,您可以根据您的选择嵌入或链接。如果评论数量预计是可管理的(相对较小),我会嵌入,如果评论太多,我会链接。
嵌入的优点是只需要一次数据库调用即可显示单个帖子/线程,并且通常可以按原样将 mongodb 响应发送到浏览器(如果客户端进行 UI 渲染)。 *注意:添加评论需要使用 $push 进行更新。请记住,comment._id 必须由您创建,MongoDB 不会自动为您创建它。嵌入式场景中注释数据的任何更新都需要使用 $ 位置运算符进行 UPDATE。*
First off, I hope you have read the docs on Schema Design, which explains with an example similar to yours.
So, you can embed or link according to your choice. I would embed if the number of comments are expected to be manageable (relatively smaller) and link if there would be too many comments.
Embedding has the advantage that only one single DB call is required to show a single post/thread and most usually can send just the mongodb response to the browser as is (if client side does the UI rendering). *Note: And adding a comment will require an UPDATE using $push. And do remember that the comment._id has to be created by you, MongoDB won't auto-create it for you. Any updates of comment data in embedded scenario will require an UPDATE with the $ positional operator.*
在这种情况下,mongoDB 注释并不像任何其他 RDBMS 那样不言自明。如您所知,MONGODB 根本不是 RDBMS。此外,由于它是序列化数据存储系统,因此您需要对每次更新添加注释,无论您使用 PHP/ASP/JAVA 还是 NODE 作为后端技术。
另请点击链接
https://docs.mongodb.com/manual/参考/operator/meta/comment/
请记住一件事 Mongo DB Jason 数组将像下面的示例一样运行
...相应地使用它...
In Such cases mongoDB commenting is not as self explanatory as any other RDBMS. As you know MONGODB not a RDBMS at all. Also as it is serialize data storage system you need put comment on every update , whatever you are using as backend technology its PHP/ASP/JAVA or NODE.
Also please follow the link
https://docs.mongodb.com/manual/reference/operator/meta/comment/
Remember one thing Mongo DB Jason array will act like this as follow with you example
etc ... Use it accordingly ...