Mongo和Meteor中嵌套类别的最佳实践 [英] best practice for nested category in Mongo and Meteor

查看:90
本文介绍了Mongo和Meteor中嵌套类别的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Mongo和Meteor框架中处理多个广告的嵌套类别.例如这样的

I want to handle nested category in Mongo and Meteor framework for several Ads. For example like this:

广告 对象的类别字段如下:

Ads object have category field like this:

MainCategory_1 > SubCategory_1.1 > SubCategory_1.1.1 > SubCategory_1.1.1.1 > { Ad_1 HERE }
               > SubCategory_1.2 > SubCategory_1.2.1
               > SubCategory_1.3 

MainCategory_2 > SubCategory_2.1 > SubCategory_2.1.1 > SubCategory_2.1.1.1 
               > SubCategory_2.2 > SubCategory_2.2.1
               > SubCategory_2.3 

例如 Ad_1 对象属于 SubCategory_1.1.1.1

for example Ad_1 object belongs to SubCategory_1.1.1.1

我也想通过以下查询访问Ad_1:

And also I want to access Ad_1 with queries like this:

All MainCategory_1 All SubCategory_1.1 All SubCategory_1.1.1 All SubCategory_1.1.1.1 还有All SubCategory3

我有两种方法:

  1. 为每个对象存储cat_id,并在多个查询中合并结果.
  2. 将类别存储为路径字符串,并在该字符串上进行查询 场地.像答案.
  1. Store cat_id for each object and merge result on multiple query.
  2. Store category as string of path and query on that string field. Like this answer.

我想知道哪个更好?

您知道其他具有更好性能和简便性的方法吗?

Do you know other approach with better performance and simplicity??

推荐答案

它高度依赖于对象之间的关系(即关系两边的对象数之比和更新频率),因此取决于根据您的应用程序和要求.

It highly depends on the relationship between your objects (i.e. the ratio of number of objects on each side of the relationship, and the frequency of updates) so it depends on your application and requirements.

MongoDB上有关非规范化的博客是一个很好的参考资源(您可以将其作为最佳实践"):

A good resource to look at (which you might take as 'best practice') is the blog from MongoDB about denormalization:

http: //blog.mongodb.org/post/87200945828/6-rules-of-thumb-for-mongodb-schema-design-part-1

http: //blog.mongodb.org/post/87892923503/6-rules-of-thumb-for-mongodb-schema-design-part-2

http: //blog.mongodb.org/post/88473035333/6-rules-of-thumb-for-mongodb-schema-design-part-3

在很短的时间内,因为它是一个广泛的主题: 我们谈论的是N-N关系,这取决于N的比率

in very short, because it's a wide subject: we're talking about N-N relationship, and it depends on the ratio of N's

如果对象是不可变的,建议您将它们嵌套在另一个对象中,因为您不必处理更新,并且搜索变得很简单.

If objects are immutable, it's recommended to nest them in another object since you won't have to deal with updates and search is made trivial.

如果对象不是一成不变的,则必须权衡更新成本与搜索集合以查找ID的成本,然后在另一个集合中搜索与该ID/该ID关联的对象的成本.

If objects are not immutable, you have to weight the cost of updates versus the cost of searching through a collection to look up ID then search through the other collection for the objects associated with that/those IDs.

这篇关于Mongo和Meteor中嵌套类别的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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