MongoDB 设计 - 标签
我是 MongoDB 新手。我有一个关于 MongoDB 性能的设计问题。假设我的电影类有两个属性:名称和导演。我还想标记这个电影类。是向此类添加一个新的 strings[] 属性,还是创建一个新类 MovieTags 更好? 我知道我会经常查询这个标签,因为我会在 UI 上使用自动完成功能。对于此自动完成功能,我只需要标签,而不是 Movie 对象。 什么选择更好?添加字符串 [] 的属性或对 MovieTags 集合的引用?考虑性能......当然,在这两种情况下都会完成索引。
我应该使用 MapReduce 吗?如果我使用嵌入 string[] 对象,则仅选择标签以用于自动完成功能?如何?
谢谢!
I'm new with MongoDB. I have a design question, about performance of MongoDB. Lets say I have the class Movies with two properties, Name and Director. Also I want to tag this Movie Class. Is better to add a new propertie of strings[] to this class, or to create a new class MovieTags? I know I will query this tags a lot because I will use an autocomplete on the UI. For this autocomplete function I only need the tags, not the Movie object.
What option is better? add a propertie of strings[] or reference to a collection of MovieTags? Thinking in performance... of course in both cases the indexing will be done.
Should I use a MapReduce? To only select the tags, for the autocomplete function if I use an embebed string[] object? How?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会使用这样的模式,它将标签存储在字符串数组字段中:
对于这种类型的查询,您不需要映射减少。通过将标签嵌入到电影文档中,您可以利用 MongoDB 的 多键 功能,并且使用单个 find() 查询查找具有给定标签的电影,如下所示:
就像你说的,还值得向多键数组添加索引以提高查询性能:
I'd probably go with a schema like this, which stores the tags in a string array field:
You wouldn't need map-reduce for this type of query. By embedding the tags inside the the movie document you can take advantage of MongoDB's multikey feature, and find movies with a given tag using single find() query like this:
And like you said, it's also worth adding an index to the multikey array to improve query performance:
您始终可以过滤作为查询结果的一部分返回的字段。
详细说明如何执行此操作的文档链接为 http:// docs.mongodb.org/manual/tutorial/query-documents/#Querying-FieldSelection
这将让您过滤掉您不感兴趣的电影对象的部分。
You can always filter the fields that are returned as part of the query result.
The link to the docs that details how to do so is http://docs.mongodb.org/manual/tutorial/query-documents/#Querying-FieldSelection
This will let you filter out parts of the movie object that you re not interested in.