title: Find on the basis of field of last element of embedded document in mongo db php
body: <p>I have a collection studentTbl which contains records like</p>
  
  <p>Record 1</p>
  
  <pre><code> "_id": ObjectId("5b45d89bbbc51e2c2c006973")
   "first_name": "Pooja",
   ...
   contact_details[
      {
       ..
      },
      {
       ..
      }
   ]
    transport_details[
      {
        allotment_id:"68998546878",
        ..
        status:"Inactive"
      },
      {
        allotment_id:"25799856890",
        ..
        status:"Active"
      }
   ]
  }
  </code></pre>
  
  <p>Record 2</p>
  
  <pre><code>  "_id": ObjectId("5b45d89bbbc51e2c2533")
   "first_name": "Poornima",
   ...
   contact_details[
      {
       ..
      },
      {
       ..
      }
   ]
    transport_details[
      {
        allotment_id:"68998546878",
        ..
        status:"Inactive"
      }
   ]
  }
  </code></pre>
  
  <p>Record 3 </p>
  
  <pre><code> "_id": ObjectId("5b45d89bbbc51e2c2c00646")
   "first_name": "Poonam",
   ...
   contact_details[
      {
       ..
      },
      {
       ..
      }
   ]
    transport_details[
      {
        allotment_id:"68998546878",
        ..
        status:"Inactive"
      },
      {
        allotment_id:"25799856890",
        ..
        status:"Active"
      }
    ]
  }
  </code></pre>
  
  <p>I am trying to tweak the below lines of code in order to fetch those students whose first_name or middle_name or last_name contains "poo" and inside the last element of embedded document transport_details, status should be "Active". How to write such a query which will find students on the name basis and traverse through the last element of embedded document transport_details and checks whether the status is active? For e.g in the above collection, pooja and poonam should be returned.</p>
  
  <p>The code is like</p>
  
  <pre><code>      // default if nothing is filled
        $query = array("schoolId"=&gt;  new MongoDB\BSON\ObjectID($this-&gt;schoolId));
  
       // if name = filled, class = null, year = null 
       if(!empty($this-&gt;name) &amp;&amp; empty($this-&gt;academicYearName) &amp;&amp; empty($this-&gt;programId))
       {
  
             $param = preg_replace('!\s+!', ' ', $this-&gt;name);
             $arg = trim($param);
  
             $query =  array(
                     '$or' =&gt; array(
                                      array("first_name" =&gt; new MongoDB\BSON\Regex($arg, 'i')),
                                      array("middle_name" =&gt; new MongoDB\BSON\Regex($arg, 'i')),
                                      array("last_name" =&gt; new MongoDB\BSON\Regex($arg, 'i')),
                                      array("registration_temp_perm_no" =&gt; $arg)
                                    ),
  
                          "schoolId"=&gt;  new MongoDB\BSON\ObjectID($this-&gt;schoolId)
                      );
  
       }
      ...
      ...
      ...
       $pipeline = array(
          array(
              '$match' =&gt; $query
          ),
          array(
              '$lookup' =&gt; array(
                  'from' =&gt; 'programTbl',
                  'localField' =&gt; 'classId',
                  'foreignField' =&gt; '_id',
                  'as' =&gt; 'ClassDetails'
              )
          ),
       );
  
      try
      {
          $cursor = $this-&gt;collection-&gt;aggregate($pipeline);
      } 
      catch (Exception $e) {
  
      }
  
      return $cursor-&gt;toArray();
  </code></pre>
  
  <p>Note that the actual code contains more conditional $query variable...</p>