5

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.

3
  • you defined $limit but not $per_page , should use $limit?
    – Fetrarij
    Commented Sep 8, 2017 at 4:11
  • sorry... that the change i forgot to make during posting. in my code $per_page is set. Let me correct the code snippet. Thanks for pointing it out.
    – echo_salik
    Commented Sep 8, 2017 at 4:17
  • I just let the script run and it is returning rows but not showing them in var_dump. Is this the default behavior because I could view the rows in var_dump when no limit/skip is applied.
    – echo_salik
    Commented Sep 8, 2017 at 4:36

1 Answer 1

2

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);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.