Search code examples
mongodbphp-mongodb

MongoDB-PHP: How to sort the result of find query by timestamp in ascending/descending order?


I want to sort the result of find query in ascending/descending order w.r.t timestamp(time_created).

My query is:

$mongoResult = $mongoDb->find(array('organization_id' => new MongoId($_SESSION['user_id'])));

Solution

  • Try:

    $mongoResult = $mongoDb->find(
      array(
        'organization_id' => new MongoId($_SESSION['user_id'])
      ))->sort(array("time_created" => -1)
    );