title: MongoDB lookup issues with performance php
body: <p>I am trying to fetch data from mongoDB using lookup</p>
  
  <p>collections companies</p>
  
  <pre><code>{
   "id" : 1,
   "company_id" : 2,
    "user_group" : 1,
   "company_name" : "xyz",
   "created_on" : "00-00-0000"
  }
  </code></pre>
  
  <p>collection users</p>
  
  <pre><code>{
    "id" : 1,
    "company_id" : 1,
    "user_group" : 1,
    "name" : "abcd",
    "email" : "abcd@abcd.abcd"
   }
  
  
  
  {
    "id" : 1,
    "company_id" : 2,
    "active": 1,
     "user_group" : 1,
    "name" : "efgh",
    "email" : "efgh@efgh.efgh"
  }
  </code></pre>
  
  <p>Query used to fetch data using php</p>
  
  <pre><code>$collection-&gt;aggregate([
          ['$match' =&gt; ['company_id' =&gt; 2]],
              ['$lookup' =&gt; [
                  'from' =&gt; 'users',
                  'localField' =&gt; 'user_group',
                  'foreignField' =&gt; 'user_group',
                  'as' =&gt; 'company_users',
              ]],
              ['$unwind' =&gt; ['path' =&gt; '$company_users', 'preserveNullAndEmptyArrays' =&gt; true]],
              ['$match' =&gt; ['$and' =&gt; [['company_users.company_id' =&gt; 2], ['company_users.active' =&gt; 1]]]],
              ['$project' =&gt; [
                  '_id' =&gt; false,
                  'company_id' =&gt; true,
                  'user_group' =&gt; true,
                  'company_users.name' =&gt; true,
                  'company_users.email' =&gt; true
              ]
          ]
      ]);
  </code></pre>
  
  <p>Query is working correctly but takes more time to retrieve data if document is greater than 1000</p>