Data Modelling in MongoDB - Multiple tables

You are here : Home / MongoDB Tutorial


Contents of page >

1) Difference in data modelling in MongoDb and RDBMS.


Data Modelling in MongoDB is much easier and flexible as compared to RDBMS.


1.1) Data modelling in MongoDb >
In MongoDb we just need one collection to store employee data. Let’s see how -


Insert in employee collection >
> db.employee.insert({id : 1,  firstName:"ankit", address : { city : "Delhi" , Country : "India"} ,  Phone : [ {PHONE_NUMBER : 1234}, {PHONE_NUMBER : 2345}]})


Now, query employee collection >
> db.employee.find().pretty();
{
       "_id" : ObjectId("587df5671e509423fa0faa64"),
       "id" : 1,
       "firstName" : "ankit",
       "address" : {
               "city" : "Delhi",
               "Country" : "India"
       },
       "Phone" : [
               {
                       "PHONE_NUMBER" : 1234
               },
               {
                       "PHONE_NUMBER" : 2345
               }
       ]
}


So, in MongoDb we just need one collection to store employee data (i.e. his address and phone numbers).




1.2) Data modelling in RDBMS >
In RDBMS we will need three tables for storing above employee data (i.e. his address and phone numbers). Let’s see how >
Here,
In employee table >
id is primary key
In address table (Here, one student can have 1 address)>
id is primary key
employee_id is foreign key (one - one relationship)
In phone table  (Here, one student can have many Phones) >
id is primary key
employee_id is foreign key (one - many relationship)

Summary -
So in this MongoDB tutorial we learned with example how to to data modelling in MongoDB. And we also learned difference in data modelling in MongoDb and RDBMS.
Data Modelling in MongoDB is much easier and flexible as compared to RDBMS.


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.


RELATED LINKS>

What is MongoDB - A quick introduction to database

Create new database in mongoDB

How to Drop collection in MongoDB

Create new collection(table) in mongoDB


Labels: MongoDB
eEdit
Must read for you :