MongoDB 设计 - 标签

发布于 2024-11-08 21:14:17 字数 346 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

郁金香雨 2024-11-15 21:14:17

我可能会使用这样的模式,它将标签存储在字符串数组字段中:

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 的 多键 功能,并且使用单个 find() 查询查找具有给定标签的电影,如下所示:

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

就像你说的,还值得向多键数组添加索引以提高查询性能:

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

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" ]
})

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 } )
无畏 2024-11-15 21:14:17

您始终可以过滤作为查询结果的一部分返回的字段。

详细说明如何执行此操作的文档链接为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文