MongoDB初学者 - 规范化还是不规范化? [英] MongoDB beginner - to normalize or not to normalize?

查看:141
本文介绍了MongoDB初学者 - 规范化还是不规范化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定,我会尝试,并使这是直接的,因为我可以。

来自mySql和思考在表,让我们使用下面的例子:

让我们假设我们有一个房地产网站,我们正在显示房子列表,
,我会使用下面的表:

*房子 - 手中的房地产资产hand
*业主 - 房子的业主 - 与房子的一对多关系

*机构 - 房地产经纪公司 - 许多多对一的关系与houses

*图像 - 与房子的多对一关系

*评论 - 与房子的多对一关系

/ p>

我知道MongoDB可以灵活地在不同的集合中设计你的web应用程序,id是一个关系数据库(标准化),并且可以快速选择可以嵌套在集合,相关对象和数据(未标准化)中。
$ $ $ $ b回到我们的房地产房屋列表,用于填充它的查询在正常的关系数据库中是相当昂贵的,对于每个房子你需要查询其图像,评论,所有者&代理,每个实体驻留在不同的表及其字段,你可能会使用连接,并有多个查询连接成一个 - 昂贵的!

输入MongoDB - 您不需要连接,您可以将房子的所有相关数据存储在房屋集合上的房子项目中,选择永远不会更快,这是一个数据库天堂! br />
哦 - 但是当你需要添加/更新/删除相关评论/代理商/所有者/图片时会发生什么?

这对我来说是一个谜,如果我需要猜测,每个相关的集合存在于它自己的收藏在其房子表格内的数据之上,并且一旦这些相关数据片段之一被添加/更新/删除,你必须更新它自己的集合,以及

在此更新时,我需要查询其他集合,以确保我用所有更新的相关数据更新房子记录吗?

我真的在这里猜猜...并感谢您的反馈
b $ b感谢您
Ajar

解决方案

尝试此方法:



确定哪个实体是英雄 p>

使用hero,我的意思是数据库的中心的实体。让我们举个例子。



$ b

b

浏览其他实体,例如所有者,代理机构,图片和评论,并询问自己是否有意义将他们的信息与房子一起。您对关系数据库中的任何外键是否有级联删除?


您将有代理(可能是所有者)的详细信息分布在多个房子。



您的房子收藏可能如下所示:

  house:{
owner,
agency,
images [],//推荐参考GridFS在这里
reviews [] //你可能不会得到太多这些为一个单独的房子
}

*实际上,它可能是房子的广告因为房屋通常在一个房地产网站上公布,这可能是你真正的感兴趣),所以只需考虑


ok, I'm going to try and make this as straight-forward as I can.
comming from mySql and thinking in tables, let's use the following example:
Let's say that we have a real-estate website and we're displaying a list of houses
normally, i'd use the following tables:
* houses - the real estate asset at hand
* owners - the owner of the house - one-to-many relationship with houses
* agencies - the real-estate broker agency - many-to-many relationship with houses
* images - many-to-one relationship with houses
* reviews - many-to-one relationship with houses

I understand that MongoDB gives you the flexibility to design your web-app in different collections with unique id's much like a relational database (normalized), and to enjoy quick selections, you can nest within a collection, related objects and data (un-normalized).
back to our real-estate houses list, the query used to populate it is quite expensive in a normal relational DB, for each house you need to query its images, reviews, owner & agencies, each entity resides in a different table with its fields, you'd probably use joins and have multiple queries joined into one - Expensive!
Enter MongoDB - where you don't need joins, and you can store all the related data of a house in a house item on the houses collection, selection was never faster, it's a db heaven!
Oh - but what happens when you need to add/update/delete related reviews/agencies/owner/images?
this is a mystery to me, and if I need to guess, each related collection exist on it's own collection on top of its data within the houses table, and once one of these pieces of related data is being added/updated/deleted you'll have to update it on it's own collection as well as on the houses collection.
Upon this update - do I need to query the other collections as well to make sure I'm updating the house record with all the updated related data?
I'm really guessing here... and appreciate your feedback
thanks
Ajar

解决方案

Try this approach:

Work out which entity (or entities) are the hero(s)

With 'hero', I mean the entity(s) that the database is centered around. Let's take your example. The hero of the real-estate example is the house*.

Work out the ownerships

Go through the other entities, such as the owner, agency, images and reviews and ask yourself whether it makes sense to place their information together with the house. Would you have a cascading delete on any of the foreign keys in your relational database? If so, then that implies ownership.

Work out whether it actually matters that data is de-normalised

You will have agency (and probably owner) details spread across multiple houses. Does that matter?

Your house collection will probably look like this:

house: {
owner,
agency,
images[], // recommend references to GridFS here
reviews[] // you probably won't get too many of these for a single house
}

*Actually, it's probably the ad of the house (since houses are typically advertised on a real-estate website and that's probably what you're really interested in) so just consider that

这篇关于MongoDB初学者 - 规范化还是不规范化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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