MongoDB中数据的非规范化 [英] Denormalization of data in MongoDB

查看:133
本文介绍了MongoDB中数据的非规范化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习MongoDB,我有一个关于数据重复的问题。在SQL世界中,您尝试规范化数据。例如,我有一个表与类别和另一个与产品。每个产品可能属于许多类别,所以这些表之间有一个联接。

I am learning MongoDB and I have a question regarding duplication of data. In the SQL world you try to normalize the data. For instance I have a table with categories and another one with products. Each product may belong to many categories so there is a join between these tables.

但是我是对的,在MongoDB你不这么认为吗?相反,每个产品都有一个嵌入的类别文档?这只是它的方式吗?

However am I right that in MongoDB you don't think like this? Does instead each product have a embedded document(s) of categories? Is that just the way it is? You don't care if the data is duplicated?

推荐答案


在SQL世界中,您尝试标准化数据

In the SQL world you try to normalize the data

不总是,对死亡点进行归一化会造成性能上的影响,但我个人不应用

Not always, normalising to the point of death inflicts performance hits but it is true that I personally do not apply the same normalisation to MongoDB as I do SQL.

如果你知道标准化的形式( http://en.wikipedia.org/wiki/Database_normalization )我喜欢将MongoDB视为1NF,然后再次向下重新归一化。

If you are aware of the normalised forms ( http://en.wikipedia.org/wiki/Database_normalization ) I like to think MongoDB as going to 1NF and then back down to denormalised again.


您不在乎数据是否重复?

You don't care if the data is duplicated?

我们的确是。

让我举个例子: category product 将是两个单独的实体,不能否认它。这两个实体已标准化(产品的重复数据已从类别中指定)。另一种思考方式是:所有产品是否只存在于一个类别中?

Let me give you an example: category and product would be two separate entities, there is no denying it. These two entities are normalised (the repeating data of product has been spearated from category). Another way of thinking of it is: Are all products only going to exist in one category?

所以在顶层实体,看到,相同的规则相对适用于1NF容易应用于MongoDB。

So on top level entities, as you can see, the same rules relatively apply with 1NF easily being applied to MongoDB.

在复制的前面,你当然不想将每个产品分别存储在每个类别(我对上面的问题回答否),所以你自然希望分离类别和产品。

On the front of duplication you, of course, would not want to store each product separately within each category (I answered no to the question above) so you would naturally want to separate catgeories and products.

这里通常会有一个多对多关系,中间有一个规范化表。这就是解除规范化的地方。你可以说一个类别将有一个对于该类别是唯一的产品列表,因此你可以将多对多关系表反标准化为类别行作为列表(或者相反地进入产品列)。这不会产生重复,因为该列表对于该类别是唯一的(更可能)。这当然意味着一个或多个类别将包含相关行的列表 _id ,而不是对象本身。

You would normally have a many-to-many relationship here with a middle normalised table. This is where de-normalisation can come in. You can say that a category will have a list of products that are unique to that category as such you could de-normalise the many-to-many relational table into the category row as a list (or the other way around into the product row). This will not generate duplication since that list is unique to that category (more than likely). This of course means that the category or products would house a list _ids of the related row instead of the object itself.

有时候,重复是不必要的,主要是为了优化或工作周围没有JOINs;

There are times where duplication is nessecary, mainly for optimisation or work arounds for not having JOINs; this rule also applies to SQL as well if you have ever done a big enough site.

复制的典型使用场景是统计数据的聚合字段,如Facebook的帖子分享和评论甚至该帖子的最新5条评论也会复制到帖子行中。

Typical usage scenarios of duplication is aggregation fields of stats like a Facebook posts shares and comments and maybe even the 5 latest comments of that post would also be duplicated onto the post row.

因此,这不是忽略模式设计的情况,而是更多地调整它MongoDBs的特点。通常,如果你这样做,你会发现你自然地设计一个好的模式。

So it is not a case of ignoring schema design but more of tuning it for MongoDBs characteristics. Normally if you do that you will find that you, naturally, design a good schema.

作为一个附加的参考,你可以参考: http://docs.mongodb.org/manual/core/data-modeling

As an added reference you can refer here: http://docs.mongodb.org/manual/core/data-modeling

这篇关于MongoDB中数据的非规范化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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