[mongodb] mongodb paging display

Time:2020-3-22

Pagination with mongodb limit and skip method
Mongodb limit() method
If you need to read a specified number of data records in mongodb, you can use mongodb’s limit method. The limit () method accepts a number parameter, which specifies the number of records read from mongodb.
Mongodb skip() method
In addition to using the limit () method to read the specified number of data, we can also use the skip () method to skip the specified number of data. The skip method also accepts a number parameter as the number of records to skip.
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)

PHP code

$page=1;
        $pageSize=1;

        $filter = [];
        //Pagination display
        $options = [
            'skip'=>($page - 1) * $pageSize,
            'limit'=>$pageSize,
            'sort' => ['createTime' => -1],
            'projection'=>['_id'=> False, "modelXML"=> False],
        ];

        $collect='.modelDraft';
        $query = new MongoDB\Driver\Query($filter, $options);
        $mongoManger = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
        $cursor = $mongoManger->executeQuery($this->dbName.$collect, $query);