Wed. Oct 16th, 2019

Codes

Codes for Coders

Mongodb left join and inner join between two collections

3 min read
mongodb database nosql tipsfusion codes rishap gandhi

mongodb database nosql tipsfusion codes rishap gandhi

mongodb database nosql tipsfusion codes rishap gandhi

As you all know MongoDB is NoSql database and is not actually not developed for relational database but still in some situations where we need to design a relational database.

So for relational database like MySql or MsSql as we all know we need to make Primary Key and Foreign Key in the database or make two or more common columns in the database. After doing this we put joins like inner, left or right join to pull the data accordingly. Same be can be done in MongoDB also but MongoDB has few restrictions. Below are following restrictions in MongoDB.

  1. You cannot make Primary Key orĀ  Foreign Key in MongoDB by default MongoDB give you a single field as Primary key but you cannot create a Foreign Key in MongoDB as MongoDB is not relational database.
  2. As you cannot create foreign key concept now you have to put the conditions of join on columns. Here is one more restrictions in MongoDB that you can join two collections only on one condition you can put two conditions till MongoDB version 3. May be in future you can get from MongoDB if it would be we will put another blog or update this one.

So left start with MongoDB joining how you can do left and inner join in MongoDB. Lets take an example by creating two collections Employee and Employee Salary.

For creating collections in MongoDB you can read the article on MongoDB : Read

mongodb create collections database nosql tipsfusion codes rishap gandhi

mongodb create collections database nosql tipsfusion codes rishap gandhi

Now as you can see we have created both collections now we will put some data in it. For inserting in collection you can read MongoDB article : Read

Now will insert some dummy data in both the collections:

mongodb insert collections database nosql tipsfusion codes rishap gandhi

mongodb find collections database nosql tipsfusion codes rishap gandhi

mongodb insert collections database nosql tipsfusion codes rishap gandhi

Now we have data in both the collections. Lets start with Joining now.

Inner Join:

What is inner join. Inner join is when we join two or more collections and it gives the common results between the collections. So lets see how we will do this in mongodb.

For joining will be use the Aggregate Functions in Mongodb (Read)

db.employee.aggregate([{
    "$lookup":{
          "from":"employee_salary",
          "localField":"_id",
          "foreignField":"_employe_id",
          "as": "employee_salary"
    },,{"$unwind":"$employee_salary"}
}]);

The above query will give you the result of inner join. Now lets see the keywords used in this query.

from: this keywords is used to tell the second collections name to which you need to join.

localField: use as primary key of the first collections i.e. salary

foreignField: use to tell on column of second collections. It like foreign key for MySql or MsSql.

as: is used to set the key in result in which data of second collection will come.

unwind: is used to pull the common records from database.

The final output will be.

mongodb aggregate inner join collections database nosql tipsfusion codes rishap gandhi

Left Join:

What is left join. It will bring all the records of left table and common records from the second table. In mongodb we can do this by using aggregate function. See the below query first.

db.employee.aggregate([{
     "$lookup":{
            "from":"employee_salary",
            "localField":"_id",
            "foreignField":"_employe_id",
            "as": "employee_salary"
     }
}]).pretty();

The only difference from the above query is that we are not using unwind (read) in this query. Lets see the result of above query.

mongodb aggregate left join collections database nosql tipsfusion codes rishap gandhi

In the above result you can see that. In first record the key employee_salary its showing two records this is because first records has two matching records in the database and in second records there is not records in employee_salary as there is no matching for the respective record.

Hope I might have cleared some of your confusion. If you still have any confusion do drop your comment below we will get back to you. If you want any specific code or mongodb drop your suggestions also.

Mongodb left join and inner join between two collections

Mongodb left join and inner join between two collections

Mongodb left join and inner join between two collections

More Stories

Leave a Reply

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