title: how to sort result of lookup in mongodb
body: <p>I want to join two collections and sort the date field in the foreign table and then have group the fields to get only one record from recent date for each device as below</p>
  
  <pre><code> $collection = $this-&gt;db-&gt;dv_device;
  
          $result_data =  $collection-&gt;aggregate(
              array('$lookup'=&gt;array('from'=&gt;'dv_device_readings', 'localField'=&gt;'device_id', 'foreignField'=&gt;'device_id','as'=&gt;'result_data')),
              array('$match'=&gt;array('user_id'=&gt;'5809c169622d10a419000029')),
              array('$sort'=&gt;array('result_data.date_time'=&gt;-1)),
              array('$group' =&gt; array(
              "_id" =&gt; array("device_id" =&gt; '$result_data.device_id'),
              "device_id" =&gt; array('$first' =&gt; '$result_data.device_id'),
              "date_time" =&gt; array('$first' =&gt; '$result_data.date_time'),
              "dc_voltage" =&gt; array('$first' =&gt; '$result_data.dc_voltage'),
              "dc_current" =&gt; array('$first' =&gt; '$result_data.dc_current'),
              "output_voltage" =&gt; array('$first' =&gt; '$result_data.output_voltage'),
              "output_current" =&gt; array('$first' =&gt; '$result_data.output_current'),
          ))
          );
  </code></pre>
  
  <p>By using the above code in php I got the below result, joining two collections is working fine but sorting and grouping is not working.</p>
  
  <pre><code>Array
  (
      [waitedMS] =&gt; 0
      [result] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [_id] =&gt; Array
                          (
                              [device_id] =&gt; Array
                                  (
                                      [0] =&gt; dv1002
                                      [1] =&gt; dv1002
                                  )
  
                          )
  
                      [device_id] =&gt; Array
                          (
                              [0] =&gt; dv1002
                              [1] =&gt; dv1002
                          )
  
                      [date_time] =&gt; Array
                          (
                              [0] =&gt; MongoDate Object
                                  (
                                      [sec] =&gt; 1477476049
                                      [usec] =&gt; 0
                                  )
  
                              [1] =&gt; MongoDate Object
                                  (
                                      [sec] =&gt; 1477476313
                                      [usec] =&gt; 0
                                  )
  
                          )
  
                      [dc_voltage] =&gt; Array
                          (
                              [0] =&gt; dv1
                              [1] =&gt; dvv2
                          )
  
                      [dc_current] =&gt; Array
                          (
                              [0] =&gt; dc1
                              [1] =&gt; dc2
                          )
  
                      [output_voltage] =&gt; Array
                          (
                              [0] =&gt; output voltage1
                              [1] =&gt; output voltage2
                          )
  
                      [output_current] =&gt; Array
                          (
                              [0] =&gt; output current1
                              [1] =&gt; output current2
                          )
  
  
                  )
  
          )
  
      [ok] =&gt; 1
  )
  </code></pre>
  
  <p>I need result as below</p>
  
  <pre><code>Array
  (
      [waitedMS] =&gt; 0
      [result] =&gt; Array
          (
              [0] =&gt; Array
                  (
  
                      [device_id] =&gt; dv1002
  
                      [date_time] =&gt; MongoDate Object
                                  (
                                      [sec] =&gt; 1477476313
                                      [usec] =&gt; 0
                                  )
                      [dc_voltage] =&gt; dvv2
                      [dc_current] =&gt; dc2
                      [output_voltage] =&gt; output voltage2
                      [output_current] =&gt; output current2
  
                  )
  
          )
  
      [ok] =&gt; 1
  )
  </code></pre>