title: How to apply $lookup in MongoDB, If the record on the second collection is deleted still give the response all the record of the first collection
body: <p>How to apply $lookup in MongoDB, If the record on the second collection is deleted still give the response all the record of the first collection as shown in the given example</p>
  <p>first collection</p>
  <pre><code>    A: [
           { _id : &quot;a&quot; ,  P : &quot;dljslfsdjf&quot; },
           { _id : &quot;b&quot; ,  P : &quot;dljslfsdjf&quot; },
           { _id : &quot;c&quot; ,  P : &quot;dljslfsdjf&quot; },
           { _id : &quot;d&quot; ,  P : &quot;dljslfsdjf&quot; }
  
  ]
  </code></pre>
  <p>Second collection</p>
  <pre><code>    B: [
       {A _id : &quot;a&quot; ,  Q : &quot;dljslfsdjf&quot; },
       {A_id : &quot;b&quot; ,   Q : &quot;dljslfsdjf&quot; },
       { A_id : &quot;c&quot; ,  Q : &quot;dljslfsdjf&quot; }
  </code></pre>
  <p>]</p>
  <h2>RESPONSE WANTED --</h2>
  <pre><code>C: [
   { _id : &quot;a&quot; ,  P : &quot;dljslfsdjf&quot;, BB:  {A _id : &quot;a&quot; ,  Q : &quot;dljslfsdjf&quot; } },
   { _id : &quot;b&quot; ,  P : &quot;dljslfsdjf&quot;, BB : {A_id : &quot;b&quot; ,   Q : &quot;dljslfsdjf&quot; } },
   { _id : &quot;c&quot; ,  P : &quot;dljslfsdjf&quot;, BB :  { A_id : &quot;c&quot; ,  Q : &quot;dljslfsdjf&quot; } },
   { _id : &quot;d&quot; ,  P : &quot;dljslfsdjf&quot;, BB: {**anything that indicates record not found**} }
  </code></pre>
  <p>]</p>
  <p>I Write a query --</p>
  <pre><code> db.A.aggregate([
   {
       $match: {
           P : &quot;dljslfsdjf&quot;
       }
   },
   {
       $lookup : {
              from : &quot;B&quot;,
              localField : &quot;_id&quot;,
              foreignField : &quot;A_id&quot;,
              as : &quot;BB&quot;
          }
   }
   {
          $unwind : &quot;$BB&quot;
   }
  </code></pre>
  <p>])</p>
  <h3>It returns only tree record, doesn't return the--</h3>
  <p><code>_id : &quot;d&quot; ,  P : &quot;dljslfsdjf&quot;, BB: {**anything that indicate record not found**}</code></p>