MongoDB $lookup In Hindi | join In MongoDB In Hindi

📔 : MongoDB 🔗

जैसा कि आप जानते ही हैं कि MongoDB कोई relational database नहीं है , जिसका मतलब है इसमें हम SQL की तरह JOIN queries नहीं होती हैं , लेकिन हाँ $lookup operator है जिसका use करके हम left join apply कर सकते हैं।

इस topic में हम $lookup के बारे में detail में समझेंगे।

What is $loop In MongoDB ?

MongoDB में , $lookup एक powerful aggregation stage है जो दो collections के बीच एक LEFT JOIN जैसा काम करता है . इसका use एक collection के documents को दुसरे collection के documents से जोड़ने के लिए किया जाता है।

जिसे एक reference field से दुसरे collection के documents को retrieve करने के लिए होता है। इसे हम aggregate() Method में use करते हैं।

MongoDB $lookup Syntax

{
   $lookup:
     {
       from : collection,
       localField : field in current document,
       foreignField: field from the "from" collection,
       as: output array field
     }
}

Syntax Explanation

  • from : ये parameter वो collection है जिससे आप join करना चाहते हैं।

  • localField : ये parameter current documents के field का नाम है , जो "from" document में stored है।

  • foreignField : ये parameter from collection के documents के field का नाम है जिसमे आपने localField की value store की है।

  • as : ये parameter एक new array field specify करता है जिसमे join किये गए data stored होंगे , मतलब joined collection का data आपको इसी नाम से access होगा।

MongoDB $lookup Example

$lookup को और अच्छे से समझने के लिए एक example देख लेते हैं।

Suppose एक "orders" collection और एक "products" collection को join करना है जिसमे कुछ इस तरह से data stored है।

orders collection :

[
   {
      _id: 1,
      order_number: "ORD001",
      product_id: 101
   },
   {
      _id: 2,
      order_number: "ORD002",
      product_id: 102
   }
]

products collection :

[
   {
      _id: 101,
      product_name: "Laptop"
   },
   {
      _id: 102,
      product_name: "Mobile Phone"
   }
]

हम orders collection से data fetch करेंगे इसलिए $lookup का use orders collections के product_id और products collections के _id field को join करने के लिए करेंगे।

db.orders.aggregate([
   {
      $lookup:
         {
           from : "products",
           localField : "product_id",
           foreignField : "_id",
           as : "ordered_products"
         }
   }
])

Output :

[
   {
      _id: 1,
      order_number: "ORD001",
      product_id: 101,
      ordered_products: [
         {
            _id: 101,
            product_name: "Laptop"
         }
      ]
   },
   {
      _id: 2,
      order_number: "ORD002",
      product_id: 102,
      ordered_products: [
         {
            _id: 102,
            product_name: "Mobile Phone"
         }
      ]
   }
]

MongoDB $lookup's SQL equivalentquery

ऊपर दिए गए example की SQL equivalent query कुछ ऐसी होगी।

SELECT orders._id, orders.order_number, products.product_name
FROM orders
INNER JOIN products ON orders.product_id = products._id;

MongoDB $unwind

$unwind MongoDB aggregation stage का use arrays को expand (unfold) करने के लिए किया जाता है।

ये एक array field को documents के रूप में अलग - अलग documents में return करता है , इससे आप array के हर element के लिए एक नया new document बनाते हैं।

ऊपर दिए गए example का output में आपको product की detail एक array में मिली है , लेकिन अगर आपको ये detail orders detail की तरह में नहीं चाहिए तो आपको $unwind का use करना पड़ेगा।

MongoDB $unwind Example

db.orders.aggregate([
   {
      $lookup:
         {
           from: "products",
           localField: "product_id",
           foreignField: "_id",
           as: "product"
         }
   },
   {
      $unwind: "$product" // Convert the "product" array to a single document.
   },
   {
      $project:
         {
           _id: 1,
           order_number: 1,
           product_name: "$product.product_name" // Extract the product name.
         }
   }
]) 

अब output कुछ इस तरह से होगा।

[
   {
      _id: 1,
      order_number: "ORD001",
      product_name: "Laptop"
   },
   {
      _id: 2,
      order_number: "ORD002",
      product_name: "Mobile Phone"
   }
]

MongoDB $lookup like Inner Join

जैसा कि आपने अभी पढ़ा कि $lookup , SQL Left Join की तरह work करता है , लेकिन कई जगह पर Inner Join की भी जरूरत पड़ सकती है।

$lookup को Inner Join की तरह use करने के लिए आपके $match Stage में एक condition add करने पड़ेगी।

Example

db.orders.aggregate([
   {
      $lookup:
         {
           from: "products",
           localField: "product_id",
           foreignField: "_id",
           as: "ordered_products"
         }
   },
   {
      $unwind: "$ordered_products" // Unwind the "ordered_products" array
   },
   {
      $match: { "ordered_products.product_name": { $exists: true } } // Match only the documents where "product_name" exists
   }
])

तो कुछ इस तरह से MongoDB में $lookup work करता है , और I hope आपको शायद इसके बारे में थोड़ा समझ आया होगा ~ :)

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers