MongoDB Data Models

Depending on the data retrieval patterns of your application and performance requirements you can have a Embedded Data Model or Normalized Data Model.

Embedded Data model

In Embedded Data model, a single document contains all related pieces of data. In RDBMS terms you call it de-normalized data. The advantage of Embedded data model is that you need fewer queries to retrieve, insert or update or delete data. Consider the following document:


   title: "Gravity",
   releaseYear: 2013,
   language: "English",
   director:"Alfonso Cuarón",
   cast: ["Sandra Bullock","George Clooney","Ed Harris", "Paul Sharma"],
   cinemas :
   	{	
	 Name: "Showcase Cinema de Lux",
	 address: "Foresters Park, Osmaston Park Rd, Derby DE23 8AG",
	 phoneNo: "0871 220 1000",
    	 showsTimes: ["13:30","18:00","21:00"]
	}
  }
 
 

In the above document you have details about a movie, and you also have a sub-document which contains details about a cinema that is showing the movie. Consider it as a one-to-one relationship that is a movie playing only in one cinema. If your application is for booking movie tickets then your query should always get movie details and cinema details. Having both these sets of information on the same document means you only need a single query to retrieve all information.

Normalized Data Model

In a Normalized Data Model, related information is stored in separate documents usually on different collections and use references to connect the documents together. In case of our movie example, you have two documents - one containing movie details and another containing cinema details. The movie document will also contain a reference field which is an array of _id field corresponding to cinemas that are showing movie.

In your movie collection you have:

	
{
  title: "Gravity",
   releaseYear: 2013,
   language: "English",
   director:"Alfonso Cuarón",
   cast: ["Sandra Bullock","George Clooney","Ed Harris", "Paul Sharma"],
   cinemas : [12345, 12346]

 }

You also have a cinema collection with details about the cinema:

  {
     _id: 12345,
     cinemaName: "Showcase Cinema de Lux",
     address: "Foresters Park, Osmaston Park Rd, Derby DE23 8AG",
     phoneNo: "0871 220 1000",
     showsTimes: ["13:30","18:00","21:00"]
   },
   
   {
    _id: 12346,
    cinemaName: "Odeon Cinema",
    address: "Goodsmoor Rd, Leicester LE72 6BD",
    phoneNo: "0871 220 1000",
    showsTimes: ["14:00","17:30","20:00"]
   }
   
 

Last Updated: 24-September-2016


Post a comment

Name

Your Comment

Email (We dont publish it)

Comments

Nothing yet..be the first to share wisdom.