Home > database >  How do in MongoDB PHP Sort
How do in MongoDB PHP Sort

Time:11-05

How to sort by desc [fillter][date] in mongoDB php

enter image description here

My code:

                    <?php
                      require_once  __DIR__  .  '/vendor/autoload.php' ;
                      $collection = (new MongoDB\Client)->API->PNR;

                      $cursor = $collection->find();
                      $i=0;
                      foreach ($cursor as $item) {
                         $i=$i 1;
                         $number=$item["data"]["data"]["number"];
                         $reservationDate=$item["data"]["data"]["reservationDate"];
                         $bookingTimeLimit=$item["data"]["data"]["bookingTimeLimit"];
                      ?>
                      

CodePudding user response:

Use this which is similar to mql

$cursor = $collection->find([], [
    'sort' => ['fillter.date' => -1]
]);

foreach ($cursor as $item) {
//process collections
}
  • Related