在 Mongo 中使用继承有什么好处?
我的应用程序有一个帖子模型,用户可以对帖子发表评论。我正在决定是使用单独的帖子和评论集合还是在帖子中嵌入评论。使用嵌入有什么优点吗?我试图找到一个原因,为什么它可能对我正在做的事情更好。
My app has a Post model and users can comment on a post. I'm deciding between using separate collections for Post and Comment or embedding comments within posts. Is there any advantage to using embed? I'm trying to find a reason why it might be better for what I'm doing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不是真正的继承,只有在某些情况下,您单独处理不同集合中的评论和帖子,并且其中一个被调用的次数远远多于另一个时,才可以很好地保持不同集合中的评论和帖子。
例如,如果我有一个网站,主页上显示帖子摘要,并且仅在访问特定帖子时显示评论。我会将它们保留为单独的集合(我什至可能不会使用 MongoDB 来存储帖子,因为简单的缓存就足够了),这样我就可以将我的 Posts 集合调整为小而快,将 Comments 集合调整为慢且大。我假设流量导致主页浏览量超过特定帖子浏览量(这并不总是正确的)。
还有一种将它们放在一起的情况,一方面它通过只有一个对象来简化我的数据模型。另一方面,我只需要一个查询来获取特定页面的所有数据,而不是使用 2 个集合进行 2 个查询。最后,我维护数据局部性,这意味着在需要时可以更有效地完成分片和 Map/Reduce。
It's not really inheritance, keeping Comments and Posts in different collections in good only if there is some situation in which you process them separately and one is called far more than the other.
For instance, if I had a site where a summary of the posts are displayed on the homepage and the comments are only displayed when accessing a specific post. I would keep them is separate collections (I would probably not even use MongoDB for the posts since simple caching would suffice) this way I can tune my Posts collection to be small and fast and my Comments collection to be slower and larger. I'm assuming that the traffic is such that homepage views are OOM more than specific Post views (this is not always correct).
There is also a case for keeping them together, for one it simplifies my data model by having only one object. For another I only require one query to fetch all the data for a specific page as opposed to 2 queries with 2 collections. And finally I maintain data locality which means sharding and Map/Reduce can be done more efficiently should the need arise.