Home >> Latest Articles >> Does MongoDB Support Join?

Does MongoDB Support Join?

MongoDB - Join MongoDB

Document-oriented databases such as MongoDB are designed to store de-normalized data.

Fortunately, MongoDB 3.2 introduces a new $lookup operator which can perform a LEFT-OUTER-JOIN-like operation on two or more collections.

As of MongoDB 3.2 the answers to this question are mostly no longer correct. The new $lookup operator added to the aggregation pipeline is essentially identical to a left outer join:

{
 $lookup:
 {
 from: collection_to_join,
 localField: field_from_the_input_documents,
 foreignField: field_from_the_documents_of_the_from_collection,
 as: output_array_field
 }
}

Since, MongoDB is not a relational database, so developers should be careful about specific use case of $lookup.

You may also like - MongoDB Interview Questions
Ref link: http://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb

How do I perform the SQL Join equivalent in MongoDB?

  1. The $lookup is introduced in the MongoDB 3.2 and it's perform as "Left Outer Join" in the same database collection.
  2. The "$lookup" stage does an equality match between a fields.
  3. The "$lookup" stage adds a new array field whose elements are the matching from the "joined" collection.
  4. The "$lookup" is an alternate way to implement Joins in MongoDB.
{
  $lookup: {
    from: "collection- to-join",
    localField: "Input-field-documents",
    foreignField: "Input-field- from - documents -of - the- "from"- collection",
    as: "Output- field-Array"
  }
}
Perform Joins using $lookup
//User Collection:

{ "_id" : 1001, "name" : "Anil Singh", "Age" : 32, "Qualification" : "MCA"}
{ "_id" : 1002, "name" : "Reena Singh", "price" : 26, "Qualification" : "MA" }
{ "_id" : 10033 }

//Customer Collection:
{ "_id" : 1001, "cust_name" : "Aradhya", description: "Customer 1", "terms" : 3 }
{ "_id" : 1002, "cust_name" : "Sunil", description: "Customer 2", "terms" : 4 }
{ "_id" : 1003, "cust_name" : "Sushil", description: "Customer 3", "terms" : 6 }
{ "_id" : 1004, "cust_name" : null, description: "Customer 4" }
{ "_id" : 1005 }

Join operations perform between the user and customer collection on the fields "name" and "cust_name".

db.user.aggregate([
  {
    $lookup: {
      from: "customer",
      localField: "name",
      foreignField: "cust_name",
      as: "joins_docs"
    }
  }
]);

Output: The Operation Result looks as.


{
  "_id": 1001,
  "name": "Anil Singh",
  "Age": 32,
  "Qualification": "MCA",
  "joins_docs": [
    {
      "_id": 1002,
      "cust_name": "Sunil",
      description: "Customer 2",
      "terms": 4
    }
  ]
}
{
  "_id": 1002,
  "name": "Reena Singh",
  "Age": 26,
  "Qualification": "MA",
  "joins_docs": [
    {
      "_id": 1003,
      "cust_name": "Sushil",
      description: "Customer 3",
      "terms": 6
    }
  ]
}
{
  "_id": 1003",
  joins_docs": [
    {
      "_id": 1004,
      "cust_name": null,
      description: "Customer 4"
    }
  ]
}
{
  "_id": 1005
}
Reference Links:
  1. https://www.mongodb.com/blog/post/joins-and-other-aggregation-enhancements-coming-in-mongodb-3-2-part-1-of-3-introduction
  2. http://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb
I hope you are enjoying with this post! Please share with you friends. Thank you!!
You may also like - Node.js Interview Questions
Full Stack Tutorials

Author @FullStackTutorials | View all Articles