Data modelling in MongoDb in java (Multiple table - primary and foreign key - one to one /one to many




Contents of page >
  • 1) Data modelling in MongoDb in java - Program to Insert big object in collection (data modelling) >
  • 2) Data modelling in MongoDb vs RDBMS >
    • 2.1) Data modelling in MongoDb >
    • 2.2) Data modelling in RDBMS >


1) Data modelling in MongoDb in java - Program to Insert big object in collection (data modelling) >
package insertDocumentInCollectionInMongoDB;
/**
* Write a program to Insert Some Complex(Big) Object and Array in collection In MongoDb in java
*/
import java.util.ArrayList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
public class InsertDocumentInCollectionInMongoDb5_InsertArrayAndSomeComplexObject {
   public static void main(String[] args) {
          /* Connect to MongoDB */
          MongoClient mongoClient =
                       new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
         
          /* Connect to DATABASE */
          //If in case database doesn't exists, it will be created at runtime
          DB db = mongoClient.getDB("mydb");
                      
          /* Get the COLLECTION (TABLE) */
          //If in case collection (Table) doesn't exists, it will be created at runtime
          DBCollection dbCollection = db.getCollection("employee");
          /*If collection already exists, you may remove everything
          from collection for perfect OUTPUT of program*/
          //dbCollection.remove(new BasicDBObject()); //Not Mandatory
                      
          /** create a BasicDBObject (document /record) */
          BasicDBObject basicDBObjectMain = new BasicDBObject();
          basicDBObjectMain.put("id", 1);  
          basicDBObjectMain.put("name", "Ankit");
         
          /** create a BasicDBObject for Address */
          BasicDBObject basicDBObject_Address = new BasicDBObject();
          basicDBObject_Address.put("city", "Delhi");
          basicDBObject_Address.put("Country", "India");
         
          /* Put Address in BasicDBObject */
          basicDBObjectMain.put("address", basicDBObject_Address);
          /** create a list of Phone */
          java.util.List<Integer> phoneList = new ArrayList<Integer>();
          phoneList.add(9899);
          phoneList.add(1234);
          /* Put phone in BasicDBObject */
          basicDBObjectMain.put("Phone", phoneList);
         
          /** INSERT document/record in COLLECTION in MongoDB */
          System.out.println("INSERT document >");
          dbCollection.insert(basicDBObjectMain);
          /* Display documents of COLLECTION in MongoDB */
          System.out.println("Display documents of COLLECTION in MongoDB >");
          DBCursor dbCursor = dbCollection.find();
          while (dbCursor.hasNext()) {
                 System.out.println(dbCursor.next());
          }
  
   }
}
/* OUTPUT
INSERT document >
Display documents of COLLECTION in MongoDB >
{ "_id" : { "$oid" : "585c104d7dbba63d58361eab"} , "id" : 1 , "name" : "Ankit" , "address" : { "city" : "Delhi" , "Country" : "India"} , "Phone" : [ 9899 , 1234]}
*/



2) Data modelling in MongoDb vs RDBMS >
2.1) Data modelling in MongoDb >
Now, let’s format output of above program to see Data modelling in MongoDb >
{  
  "_id":ObjectId("587374e41ed67b77e9efbc69"),
  "id":1,
  "firstName":"ankit",
  "address":{  
     "city":"Delhi",
     "Country":"India"
  },
  "Phone":[  
     9899,
     1234
  ]
}
So, in MongoDb we just used one collection to store employee data (i.e. his address and phone numbers)

2.2) Data modelling in RDBMS >
But, 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 >
id is primary key
employee_id is foreign key (one - one relationship)
In phone table >
id is primary key
employee_id is foreign key (one - many relationship)



Download >
Now, it’s time to Download maven Project :) Click here.

Read : How to Import Maven project in eclipse

OR



For in depth understanding read >

Relationship in MongoDB

1 - 1 Relationship in MongoDB - One to One


1 - Many Relationship in MongoDB - One to Many


Many - 1 relationship in MongoDB - Many to One


Many-Many relationship in MongoDB



Summary -
So in this MongoDB tutorial we learned with example and program how to to data modelling in MongoDB in java. 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>




eEdit
Must read for you :