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

查看:64
本文介绍了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中,我应该只在Topics集合中包括一个Comments集合,还是将其放在单独的集合中并按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中.如果是多对多,您可能会选择使用更传统的关系,也可能会选择将双方作为ILists嵌入.

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+ Subfield + of + Fields )

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来实现.例如,要查找当前正在使用的所有标签(按其使用频率排序),您可以映射发出该标签中所使用标签的每个Topic,然后将其缩减以得到所需的结果.然后,您可以永久地存储该地图的结果,并仅在需要时才更新它.

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天全站免登陆