mongo 中哪个更好?将子项放入数组或多个字段中 [英] Which one is better in mongo? Putting children in an array or in a number of fields

查看:39
本文介绍了mongo 中哪个更好?将子项放入数组或多个字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MongoDB,它作为多个分离的 SQL 数据库和 API 的整合中间层运行.

I have a MongoDB running as a consolidating middle layer of a number of separated SQL databases and an API.

这些 SQL 数据库共享相似的文章表,但结构不同.鉴于一篇文章将在多个类别和子类别下,不超过 4 层.

Those SQL databases share similar Article table but in different structure. Given that an Article will under a number of category and subcategory with not more than 4 layers.

事实证明我们有 2 个模式设计.

Turns out that we have 2 schema designs.

<代码>{类别: [...]}

<代码>{类别L1:xxx,类别L2:xxx,类别L3:xxx,类别L4:xxx}

那么问题来了,哪一个在性能和可维护性上更好.

So the question is, which one is better in performance and maintainability.

推荐答案

标题问题的答案(最好将子项放在数组或字段中)几乎总是取决于您的上下文.

The answer to the title question (better to put children in an array or in fields) pretty much always depends on your context.

在您的情况下,看起来我们不是在谈论儿童,而是在谈论分层分类法,有一个基本类别,然后是一个子类别,依此类推,分为四个级别.我会发现很难证明将其存储为数组是合理的,因为该数组意味着所有成员的价值大致相同,除了它们的排序.正如@Thilo 在评论中指出的那样,字段结构还使查询和索引变得稍微容易一些.

In your case, it looks like we are not talking about children, but about a layered taxonomy, with a base category, then a subcategory, and so on, for four levels. I would find it hard to justify storing this as an array, as the array would imply that all members are roughly equally valuable, except for their ordering. As @Thilo points out in a comment, the field structure also makes it slightly easier to query and index.

但在我看来,使用字段最重要的好处是您可以明确每个数据点的含义.如果一年后另一位开发人员查看 categories 数组,他们可能会认为他们正在处理一组标签,并且会错过分类的分层结构.另一方面,如果开发者查看一组字段,每个类别一个,正确命名,他们将有更好的机会理解您的意图.

But the most important benefit of using fields, from my point of view, is that you make explicit the meaning of each data point. If a year down the line another developer looks at a categories array, they'll probably assume that they are dealing with a set of labels, and will miss the layered structure of your classification. If, on the other hand, the developer looks at a set of fields, one per category, properly named, they will have a better chance at understanding your intent.

这篇关于mongo 中哪个更好?将子项放入数组或多个字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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