MongoDB PHP - Limit and Skip not Working

4.7k views Asked by At

I am trying to limit a query return length with limit and skip but the returned array is an empty array. Here is the code snippet,

$start = 0;
$limit = 10;
$options = [
    'skip' => $start,
    'limit' => $limit,
];
$return = $db->collection->find([], $options);
  • Collection has data (243 documents/rows to be exact)
  • only find([]) does return those rows
  • Im using mongodb/mongodb library via composer
  • MongoDB PHP Plugin is the new one, installed via PECL mongodb-1.2.9

Is there something I am doing wrong? Is this a bug or a random thing?

Regards

PS: I know this is an already asked question, but most of the answers are related to the older extensions.

1

There are 1 answers

0
Niklas Ekman On

According to the official Mongo DB documentation what you have done is correct.

The second $options argument to the find($filter, $options) method supports the limit and skip keys, e.g.:

$options = [
    "limit" => 10,
    "skip" => 0
];

$results = $mongoCollection->find([], $options);