MongoDB:您是否仍应提供链接到其他集合的 ID 或仅包含集合? [英] MongoDB: Should you still provide IDs linking to other collections to or just include collections?

查看:8
本文介绍了MongoDB:您是否仍应提供链接到其他集合的 ID 或仅包含集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总的来说,我对 MongoDB 和 NoSQL 还是很陌生.我有一个主题集合,每个主题可以有很多评论.每条评论都会有元数据和其他使评论集合有用的东西.

I'm pretty new to MongoDB and NoSQL in general. I have a collection Topics, where each topics can have many comments. Each comment will have metadata and whatnot making a Comments collection useful.

在 MySQL 中,我会使用外键链接到 Comments 表,但在 NoSQL 中,我应该只将 Comments 集合包含在 Topics 集合中,还是将其放在单独的集合中并通过 id 链接?

In MySQL I would use foreign keys to link to the Comments table, but in NoSQL should I just include the a Comments collection within the Topics collection or have it be in a separate collection and link by ids?

谢谢!马特

推荐答案

视情况而定.

这取决于您希望拥有的每种类型的对象的数量.您能否将它们全部放入给定主题的单个 MongoDB 文档中?应该不会吧.

It depends on how many of each of these type of objects you expect to have. Can you fit them all into a single MongoDB document for a given Topic? Probably not.

这取决于关系 - 您是一对多还是多对多关系?如果它是一对多且相关实体的数量很少,您可能会选择将它们嵌入到文档的 IList 中.如果是多对多,您可能会选择使用更传统的关系,或者您可能选择将双方都嵌入为 IList.

It depends on the relationships - do you have one-to-many or many-to-many relationships? If it's one-to-many and the number of related entities is small you might chose to put embed them in an IList on a document. If it's many-to-many you might chose to use a more traditional relationship or you might chose to embed both sides as ILists.

您仍然可以在 MongoDB 中使用单独的集合对关系进行建模,但数据库中没有连接,因此您必须在代码中执行此操作.从性能的角度来看,加载一个主题然后加载它的评论可能会很好.

You can still model relationships in MongoDB with separate collections BUT there are no joins in the database so you have to do that in code. Loading a Topic and then loading the Comments for it might be just fine from a performance perspective.

其他提示:

使用 MongoDB,您可以索引文档上的 INTO 数组.因此,不要将索引视为文档(如 SQL)上简单字段的索引.例如,您可以在主题上使用标签集合并索引标签.(参见 http://www.mongodb.org/display/DOCS/Indexes#Indexes-Arrays)

With MongoDB you can index INTO arrays on documents. So don't think of an Index as just being an index on a simple field on a document (like SQL). You can use, say, a Tag collection on a Topic and index into the tags. (See http://www.mongodb.org/display/DOCS/Indexes#Indexes-Arrays)

当您检索或写入数据时,您可以对任何文档进行部分读取和部分写入.(见 http://www.mongodb.org/display/DOCS/Retrieving+a+子集+of+字段)

When you retrieve or write data you can do a partial read and a partial write of any document. (see http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields)

最后,当您看不到如何使用集合和索引获取所需内容时,您也许可以使用 map reduce 来实现.例如,要查找当前使用的所有标签(按其使用频率排序),您将映射每个主题发射其中使用的标签,然后您将减少该集合以获得您想要的结果.然后,您可以永久存储该 map reduce 的结果,并仅在需要时对其进行更新.

And, finally, when you can't see how to get what you want using collections and indexes, you might be able to achieve it using map reduce. For example, to find all the tags currently in use sorted by their frequency of use you would map each Topic emitting the tags used in it, and then you would reduce that set to get the result you want. You might then store the result of that map reduce permanently and only up date it when you need to.

这是对关系思维的重大转变,但如果您需要 NOSQL 方法带来的可扩展性和灵活性,那么它是值得的.

It's a fairly significant mind-shift from relational thinking but it's worth it if you need the scalability and flexibility a NOSQL approach brings.

这篇关于MongoDB:您是否仍应提供链接到其他集合的 ID 或仅包含集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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