H
H
HellWalk2018-06-13 16:17:24
MongoDB
HellWalk, 2018-06-13 16:17:24

How to make a selection of records with the latest?

There is a request:

//...
    public static function select($filter = [], $options = [])
    {
        self::connect();
        $query = new MongoDB\Driver\Query($filter, $options);
        return self::$manager->executeQuery(self::$collection, $query);
    }
//...

foreach (MongoDB::select([], ['limit' => 3]) as $frame) {
    echo '<p>' . $frame->name . '</p>';
}

Displays the last three entries in the collection. Everything works, but the first three collections are selected, and it is necessary that the last 3 are selected, with the appropriate sorting.
I read about sorting in MongoDB, I met such examples: But I don’t understand what needs to be written to sort it the way I need. Update Added some entries with 'time' => mictorime(true) and the following selection works on them: MongoDB::select([], ['sort' => ['time' => -1], 'limit' => 5]) Accordingly, the conclusion suggests itself - without the corresponding field, over time, sorting from the end will not work in any way?
$options = ['sort' => ['timestamp' => -1]];

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question