Relationship Between Collections/Tables: MongoDB


In this video tutorial we shall learn about normalization, and the way mongoDB handles relationship between two collections(Primary Key and Foreign Key concept in RDBMS)

Normalization: It refers to the process of organizing the data to minimize redundancy and dependency.

There is no 1 way of doing this in mongoDB. You could implement it according to the needs of your application.

For Example, Normalization is good in the application wherein we do more of write operation. De-normalization is good, wherein we only read.

In today’s tutorial, we shall see how we can implement relationship between two collections/tables and query the database using this relationship.

Database name: company
Collections: info, ceo

mongoDB-database-collection

info collection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>use company
switched to db company
 
>db.info.find().forEach(printjson)
{
"_id" : ObjectId("517e829d005b19f1f0d96b25"),
"name" : "Apple",
"product": "iPhone5S",
"emp_no" : 100
}
 
{
"_id" : ObjectId("517e8377005b19f1f0d96b26"),
"name" : "Technotip",
"product": "Video Tutorials - Educational",
"emp" : [
"Satish",
"Kiran"
],
"Videos" : {
"mongo" : "MongoDB Videos",
"php": "PHP Video Tutorials"
}
}

Create another collection ceo and insert a document

1
2
3
4
5
6
7
8
9
10
>db.ceo.insert({
      name      : "SATISH",
      company_id: db.info.find()[1]._id
});
>db.ceo.find().forEach(printjson)
{
        "_id" : ObjectId("51822e2c60e925795355fd26"),
        "name" : "SATISH",
        "company_id" : ObjectId("517e8377005b19f1f0d96b26")
}

Here we store the _id value present in the second record of info collection inside first record’s company_id field of ceo collection.
This way we build a relation between two collections info and ceo.

Next, we query mongoDB to get some results based on this relationship.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> db.info.find({ _id: db.ceo.find()[0].company_id }).forEach(printjson)
{
        "_id" : ObjectId("517e8377005b19f1f0d96b26"),
        "name" : "Technotip",
        "product" : "Video Tutorials - Educational",
        "emp" : [
                "Satish",
                "Kiran"
        ],
        "videos" : {
                "mongo" : "MongoDB Videos",
                "php" : "PHP Video Tutorials"
        }
}
>

This command(in first line) retrieves the document in the ‘info’ collection which matches the company_id value present in ceo collection and _id of info collection.

Primary Key and Foreign Key Concept in MongoDB



YouTube Link: https://www.youtube.com/watch?v=7cYT0Qbu9tU [Watch the Video In Full Screen.]



Note: In traditional database systems(RDBMS) we accomplish the same by using primary key and foreign key concept. And make use of JOINS (Equi JOIN, RIGHT JOIN, LEFT JOIN) to query the database, which actually consumes more time and resources. In mongoDB, there is no need of JOIN’s and hence is faster than RDBMS.

4 thoughts on “Relationship Between Collections/Tables: MongoDB”

Leave a Reply to Akhil Cancel reply

Your email address will not be published. Required fields are marked *