2

I'm trying to use options in MongoDB\Driver\Query:

$options = array();
...
if (isset($limit['page']))
    {$options['skip']=($limit['page'] - 1) * $limit['hpp'];}//20
if (isset($limit['hpp']))
    {$options['limit']=$limit['hpp'];//20
    $options['batchSize']=$limit['hpp'];}//20
print('<br />--------$options :<br />');
print_r($options);
$query = new MongoDB\Driver\Query($search, $options);
print('<br />********$query :<br />');
echo '<pre>';
print_r($query);
echo  '</pre>';

If $options seems ok:

--------$options :
Array ( [sort] => Array ( [_id] => 1 ) [skip] => 20 [limit] => 20 [batchSize] => 20 )

$query doesn't give me the good options:

********$query :  
MongoDB\Driver\Query Object  
(  
[query] => stdClass Object  
    (  
        [$orderby] => stdClass Object  
            (  
                [_id] => 1  
            )  
         [$query] => stdClass Object  
            (  
            )  
     )  
[selector] =>  
[flags] => 0  
[skip] => 20  
[limit] => 0  
[batch_size] => 0  
[readConcern] =>  
)

limit and batch_size are not equals to 20, why? and how to do, please?

Thanks in advance

3
  • The query object looks very old. Something like v1.0 of the driver. What versions you are using? php, mongodb, and the driver. Commented Feb 19, 2018 at 14:18
  • why old? php7, mongodb 1.2.6 driver=? Commented Feb 19, 2018 at 15:04
  • The Query should be like filter=>Object, options=>Object, readConcern=>Object, see SaschaM78's answer. Your print_r($query); returns options on the top level, not within the options object. IIRC it used to be like that a long time ago. Commented Feb 19, 2018 at 15:14

1 Answer 1

1

Tested on PHP 7.2.2:

<?php
$options= [
    'skip' => 20,
    'limit' => 20,
    'batchSize' => 20   
];

$filter= [
'_id' => '5a8adcf335f6d112d00e46c8'
];

$query= new MongoDB\Driver\Query($filter, $options);
print_r($query);

outputs:

MongoDB\Driver\Query Object ( [filter] => stdClass Object ( [_id] => 5a8adcf335f6d112d00e46c8 )

 [options] => stdClass Object
    (
        [batchSize] => 20
        [skip] => 20
        [limit] => 20
    )

[readConcern] =>  )

Solution (taken from comments)

The parameters passed as options to the Query instance are treated in a type-sensitive manner, in the author's case the parameters were passed from user input and transferred as "string" rather than "number" as excepted. The solution was to use intval() on the parameters which solved the problem.

In case things still don't work as expected - update MongoDB

I checked the changelog of the MongoDB PECL repo but couldn't find when the bug was fixed. I would recommend updating to the most current version and test again.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, well, if I write $options= [ 'skip' => 20, 'limit' => 20, 'batchSize' => 20 ]; It is ok: [flags] => 0 [skip] => 20 [limit] => 20 [batch_size] => 20 but I don't understand why my first try doesn't work...
I can only assume that it's due to your variables being initialised from user input (i.e. provided as GET or POST parameters) rather than statically set values. Could you post the output of var_dump($options);, after that change your code to use intval($limit['hpp']); and try again?
Thanks, intval() works :) without it, var_dump gives: array(4) { ["sort"]=> array(1) { ["_id"]=> int(1) } ["skip"]=> int(20) ["limit"]=> string(2) "20" ["batchSize"]=> string(2) "20" }... strings weren't here my friends. Thanks again
You're welcome & great it works! And thanks for accepting! I'll add the final solution to my answer, perhaps somebody else will also fall over this :-).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.