1 - 1 Relationship in MongoDB - One to One

You are here : Home / MongoDB Tutorial



Contents of page >


1) (1-to-1) One-to-One Relationships with Embedded document >


Embed the ADDRESS document in the STUDENT document. (I will recommend you to go for this approach)
It helps in fetching all student and address document in one query.
1.1) Create collection in MongoDB >


STEP 1.1) create and insert in STUDENT collection >
db.STUDENT.insert({
 "_id": 1,
 "FIRST_NAME": "Ankit",
 "ADDRESS": {"CITY": "Delhi"}
})
db.STUDENT.insert({
 "_id": 2,
 "FIRST_NAME": "Sam",
 "ADDRESS": {"CITY": "London"}
})


When to use this approach?


When number of  transactions are too high and need to be done atomically >  
This can be used when address is fetched too frequently. i.e (Read and write operations are too high).


MongoDB does not support transactions on multiple document.But, in MongoDB you can perform atomic operations on a single document.
So, while designing your database and collections you must try and ensure that all the related data (as much as possible) which is needed to be updated atomically must be placed in single document as embedded documents (in form of nest arrays OR nested documents)


1.2 > Now, let’s read/query/find in above MongoDB collection >


Query 1.2.1 > Query to show all students
db.STUDENT.find().pretty()


Output>
{ "_id" : 1, "FIRST_NAME" : "Ankit", "ADDRESS" : { "CITY" : "Delhi" } }
{ "_id" : 2, "FIRST_NAME" : "Sam", "ADDRESS" : { "CITY" : "London" } }


Query 1.2.2 > Query to find address of student with FIRST_NAME=”Ankit”
db.STUDENT.find({"FIRST_NAME":"Ankit"}).pretty();

Output>
{ "_id" : 1, "FIRST_NAME" : "Ankit", "ADDRESS" : { "CITY" : "Delhi" } }


2) (1-to-1) One-to-One Relationships with Document Reference >
Create separate STUDENT and ADDRESS collections.
Where documents in STUDENT contain a reference to the ADDRESS document.


2.1) Create collections in MongoDB >


STEP 2.1.1) create and insert in ADDRESS collection >
db.ADDRESS.insert({
 "_id": 11,
 "CITY": "Delhi"
})
db.ADDRESS.insert({
 "_id": 12,
 "CITY": "London"
})


STEP 2.1.2) create and insert in STUDENT collection >
db.STUDENT.insert({
 "_id": 1,
 "FIRST_NAME": "Ankit",
 "ADDRESS_ID": {
     "$ref": "ADDRESS",
     "$id": 11,
     "$db": "mydb"
  }
})
db.STUDENT.insert({
 "_id": 2,
 "FIRST_NAME": "Sam",
 "ADDRESS_ID":  {
     "$ref": "ADDRESS",
     "$id": 12,
     "$db": "mydb"
  }
})

2.2) Now, let’s see above one-one relationship of mongoDB collections in RDBMS (relational database) >


2.2.1) One-One (1-1) Relationship - Table structure in RDBMS >


2.2.2) Sql script to create above tables in RDBMS (in oracle) >
create table ADDRESS (ID number PRIMARY KEY,
                   CITY varchar2(22) );

create table STUDENT (ID number PRIMARY KEY,
                   FIRST_NAME varchar2(22),
                   ADDRESS_ID number UNIQUE,
                   FOREIGN KEY (ADDRESS_ID) REFERENCES ADDRESS (ID));


2.2.3) Let’s see tables after inserting data in RDBMS >
Here, 1 student have 1 address, and each student have unique address.


2.3 > Now, let’s read/query/find in above (2.1) MongoDB collections >


Query 2.3.1 > Query to show all students in MongoDB
db.STUDENT.find().pretty()
Output>
{ "_id" : 1, "FIRST_NAME" : "Ankit", "ADDRESS_ID" : 11 }
{ "_id" : 2, "FIRST_NAME" : "Sam", "ADDRESS_ID" : 12 }


Query 2.3.2 > Query to show all address in MongoDB
db.ADDRESS.find().pretty()
Output>
{ "_id" : 11, "CITY" : "Delhi" }
{ "_id" : 12, "CITY" : "London" }


IMPORTANT Query 2.3.3 > Query to Find address of student with FIRST_NAME=”Ankit”
var student = db.STUDENT.findOne({"FIRST_NAME":"Ankit"})
var studentAddress = student.ADDRESS_ID
db[studentAddress.$ref].find({"_id":(studentAddress.$id)})
Output>
{ "_id" : 11, "CITY" : "Delhi" }


3) Now let’s cover above point (i.e. 2nd point) - (Inserting related documents in same collection) - One-to-One Relationships with Document Reference >


3.1) create and insert in STUDENT collection (Also insert related data i.e. data (documents) of ADDRESS in STUDENT collection ) >
db.STUDENT.insert({
 "_id": 11,
 "CITY": "Delhi"
})
db.STUDENT.insert({
 "_id": 12,
 "CITY": "London"
})


db.STUDENT.insert({
 "_id": 1,
 "FIRST_NAME": "Ankit",
 "ADDRESS_ID": 11
})
db.STUDENT.insert({
 "_id": 2,
 "FIRST_NAME": "Sam",
 "ADDRESS_ID":  12
})


4) Summary -


So in this mongoDB tutorial we learned how can we create
1-1 (one to one) Relationship in MongoDB with Embedded document  and Document Reference.


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 :