MongoDB beginner - to normalize or not to normalize?

database, mongodb, non-relational-database, nosql

Solution

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

Problem

I'm going to try and make this as straight-forward as I can. Coming from MySQL and thinking in terms of 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 IDs 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! 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 its 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 its 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 just guessing here and would really appreciate your feedback. Thanks, Ajar

Original source