MongoDB的设计 - 标签 [英] MongoDB design - tags

查看:203
本文介绍了MongoDB的设计 - 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新与MongoDB的。我有一个设计问题,有关MongoDB的性能。可以说我有类电影有两个属性,名称和董事。此外,我想这个标签Movie类。不如字符串[]的新propertie加入到这一类,或创建一个新的类MovieTags? 我知道我会查询这个标签了很多,因为我会在用户界面上使用自动完成。对于这个自动完成功能我只需要在标签,而不是Movie对象。 什么选项是更好吗?添加字符串[]或引用的propertie到MovieTags集合?在性能上在这两种情况下的索引将进行思考......当然。

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.

我应该用一个麻preduce?如果仅选择标记,用于自动完成功能,如果我用一个embebed的String []对象?怎么样?

Should I use a MapReduce? To only select the tags, for the autocomplete function if I use an embebed string[] object? How?

谢谢!

推荐答案

我可能会用这样的模式,它存储了标签的字符串数组现场去:

I'd probably go with a schema like this, which stores the tags in a string array field:

db.movies.insert({
    name: "The Godfather",
    director: "Francis Ford Coppola",
    tags: [ "mafia", "wedding", "violence" ]
})

db.movies.insert({
    name: "Pulp Fiction",
    director: "Quentin Tarantino",
    tags: [ "briefcase", "violence", "gangster" ]
})

db.movies.insert({
    name: "Inception",
    director: "Christopher Nolan",
    tags: [ "dream", "thief", "subconscious" ]
})

您将不需要地图,减少对这种类型的查询。通过嵌入标签的电影文件里面你可以利用MongoDB的多键功能,并发现使用单找到()与给定的标签的电影像这样查询:

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:

db.movies.find( { tags: "dream" } )

和像你说的,这也是值得添加索引到多键阵列来提高查询性能:

And like you said, it's also worth adding an index to the multikey array to improve query performance:

db.movies.ensureIndex( { tags: 1 } )

这篇关于MongoDB的设计 - 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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