0

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"];
                      ?>
                      
1
  • You could let the MongoDB server do the sorting and project the fields you want. This might simplify and speed up your PHP client side code. Commented Nov 5, 2022 at 4:56

1 Answer 1

1

Use this which is similar to mql

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

foreach ($cursor as $item) {
//process collections
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.