sokil / php-mongo

MongoDB ODM. Part of @PHPMongoKit
http://phpmongokit.github.io/
MIT License
242 stars 46 forks source link

Get one document with sorting #172

Closed MCubs closed 5 years ago

MCubs commented 5 years ago

Hello,

How I get one document with use Cursor method ->sort(). Method ->one() don`t using ->sort(), and methot ->current() deprecated.

sokil commented 5 years ago

Method Cursor::one() is frapper for findOne, see https://docs.mongodb.com/manual/reference/method/db.collection.findOne/. Due to the manual:

If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk.

So if you need first document from sorted set, use something like ->sort([])->limit(1)->all()->current()