{"id":42191,"date":"2016-11-17T10:34:42","date_gmt":"2016-11-17T05:04:42","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=42191"},"modified":"2024-01-02T17:44:29","modified_gmt":"2024-01-02T12:14:29","slug":"the-most-awaited-feature-sql-joins-is-now-available-in-mongodb-3-2","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/the-most-awaited-feature-sql-joins-is-now-available-in-mongodb-3-2\/","title":{"rendered":"The Most Awaited Feature &#8220;SQL Joins&#8221; is Now Available in MongoDB 3.2"},"content":{"rendered":"<p>SQL Joins are used to combine documents\/rows from 2 or more tables based upon common field present in them.<\/p>\n<p>MongoDB 3.2 launched the most awaited feature &#8220;Joins&#8221; which is supported in SQL database however was not present in the earlier version of<a title=\"MEAN Stack Development\" href=\"http:\/\/www.tothenew.com\/mean-stack-web-development-consulting\" target=\"_blank\" rel=\"noopener\"> MongoDB<\/a>. This feature will change the way you design your database schema and application using MongoDB.<\/p>\n<p>Now you can run left-outer equi-joins query using $lookup operator in the MongoDB Aggregation Framework. With the help of Joins you can improve your application&#8217;s performance by reducing the number of queries on MongoDB.<\/p>\n<p><strong>What is a Left Outer Equi-Join?<\/strong><\/p>\n<p>Left Outer Equi-Join will have all data from left collection and only matching data from right collection.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/image00-9a0e627f57.png\" alt=\"\" width=\"380\" height=\"285\" \/><\/p>\n<p>$lookup operator performs a left outer join to an unsharded collection in the same database to filter in documents from the \u201cjoined\u201d collection for processing.<\/p>\n<p>The $lookup operator has the following syntax:<\/p>\n<p>[sourcecode language=&#8221;css&#8221;]<br \/>\n{<br \/>\n   $lookup:<br \/>\n     {<br \/>\n       from: &lt;collection to join&gt;,<br \/>\n       localField: &lt;field from the input documents&gt;,<br \/>\n       foreignField: &lt;field from the documents of the &quot;from&quot; collection&gt;,<br \/>\n       as: &lt;output array field&gt;<br \/>\n     }<br \/>\n}<\/p>\n<p>[\/sourcecode]<\/p>\n<p>Let me explain join with an example.<\/p>\n<p><strong>Orders collection:<\/strong><\/p>\n<p>[sourcecode language=&#8221;css&#8221;]<br \/>\n{<br \/>\n    &quot;_id&quot; : 2.0,<br \/>\n    &quot;items&quot; : [<br \/>\n        3.0,<br \/>\n        4.0<br \/>\n    ]<br \/>\n}<\/p>\n<p>[\/sourcecode]<\/p>\n<p><strong>Products collection:<\/strong><\/p>\n<p>[sourcecode language=&#8221;css&#8221;]<br \/>\n{<br \/>\n    &quot;_id&quot; : 3.0,<br \/>\n    &quot;item_name&quot; : &quot;Desi Ghee&quot;,<br \/>\n    &quot;item_price&quot; : 500.0,<br \/>\n    &quot;quantity&quot; : &quot;2 kg&quot;<br \/>\n}<\/p>\n<p>{<br \/>\n    &quot;_id&quot; : 4.0,<br \/>\n    &quot;item_name&quot; : &quot;Namkeen&quot;,<br \/>\n    &quot;item_price&quot; : 50.0,<br \/>\n    &quot;quantity&quot; : &quot;1 pkt&quot;<br \/>\n}<\/p>\n<p>[\/sourcecode]<\/p>\n<p>$lookup can be used in conjunction with other pipeline operators in aggregation framework to make a lookup into cross collection ( i,e left outer joins).<\/p>\n<p>Below Query will first <a title=\"$unwind\" href=\"https:\/\/docs.mongodb.com\/v3.2\/reference\/operator\/aggregation\/unwind\/\">unwind<\/a> the items presents in order collection and make a join with products collection by checking items equals to _id .<\/p>\n<p>[sourcecode language=&#8221;css&#8221;]<br \/>\ndb.orders.aggregate([<br \/>\n    {<br \/>\n      $unwind: &quot;$items&quot;<br \/>\n   },<br \/>\n    {<br \/>\n      $lookup:<br \/>\n        {<br \/>\n          from: &quot;products&quot;,<br \/>\n          localField: &quot;items&quot;,<br \/>\n          foreignField: &quot;_id&quot;,<br \/>\n          as: &quot;embeddedData&quot;<br \/>\n        }<br \/>\n   }<br \/>\n])<\/p>\n<p>[\/sourcecode]<\/p>\n<p><strong>The output of above MongoDB query is:<\/strong><\/p>\n<p>[sourcecode language=&#8221;css&#8221;]<br \/>\n\/* 1 *\/<br \/>\n{<br \/>\n    &quot;_id&quot; : 2.0,<br \/>\n    &quot;items&quot; : 3.0,<br \/>\n    &quot;products&quot; : [<br \/>\n        {<br \/>\n            &quot;_id&quot; : 3.0,<br \/>\n            &quot;item_name&quot; : &quot;Desi Ghee&quot;,<br \/>\n            &quot;item_price&quot; : 500.0,<br \/>\n            &quot;quantity&quot; : &quot;2 kg&quot;<br \/>\n        }<br \/>\n    ]<br \/>\n}<\/p>\n<p>\/* 2 *\/<br \/>\n{<br \/>\n    &quot;_id&quot; : 2.0,<br \/>\n    &quot;items&quot; : 4.0,<br \/>\n    &quot;products&quot; : [<br \/>\n        {<br \/>\n            &quot;_id&quot; : 4.0,<br \/>\n            &quot;item_name&quot; : &quot;Namkeen&quot;,<br \/>\n            &quot;item_price&quot; : 50.0,<br \/>\n            &quot;quantity&quot; : &quot;1 pkt&quot;<br \/>\n        }<br \/>\n    ]<br \/>\n}<\/p>\n<p>[\/sourcecode]<\/p>\n<p>I hope this is helpful.<\/p>\n<p>For more details check &#8211; https:\/\/docs.mongodb.com\/v3.2\/reference\/operator\/aggregation\/lookup\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL Joins are used to combine documents\/rows from 2 or more tables based upon common field present in them. MongoDB 3.2 launched the most awaited feature &#8220;Joins&#8221; which is supported in SQL database however was not present in the earlier version of MongoDB. This feature will change the way you design your database schema and [&hellip;]<\/p>\n","protected":false},"author":943,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[4843,3781,1596,1900,1252,2683,558],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42191"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/943"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=42191"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42191\/revisions"}],"predecessor-version":[{"id":59820,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42191\/revisions\/59820"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=42191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=42191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=42191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}