'Limit and Sort don't both work when used together mongodb php

Obligatory - I know this question has been asked before, but answers back then relate to the old mongoldb driver, I'm using the pecl mongodb php library.

The aim of this query is to find the 3 users with the most likes(Int32). Multiple documents contain arrays(data) with the latest(array element as object) name(string) updated.

From what I have tried here, I can either sort the data or limit it, both can't be applied.

    <?php
//Pecl Mongodb library
require_once __DIR__ . '/vendor/autoload.php';
//conect to Docker container
$conn = new MongoDB\Client('mongodb://172.17.0.3:27017');
$db = $conn->mydb;
$collection = $db->mycol;
//this query is to search for all users
$badquery = array('$ne'=> '1');
$query = array('user'=> $badquery);
$projection =  array('projection' => ["_id" => 0, "likes" => 1, "data.name" => 1, "user" => 1, 'data' => ['$slice' => -1]]);
$sort = ['sort' => ['likes' => -1]];
$limit = ['limit' => 3];
//$cursor = $collection->find($query, $limit, $options, $projection);
$cursor = $collection->find($query, $sort, $projection, $limit);
foreach ($cursor as $document) {
    try {
      //var_dump($document);
      echo "<br/> <br/> <br/> ";
        echo $document['data']['0']['name'] . "<br/>";
   } catch (Exception $e) {
       echo 'Caught exception: ',  $e->getMessage(), "\n";
   }
 }
 ?>

When I move the $limit in the line $cursor = $collection->find($query, $sort, $projection, $limit); the output changes to either only display 3 (unsorted) names or to show all sorted names. This can be tested by inverting the sort order $sort = ['sort' => ['likes' => 1]]; am I missing something or can others both sort and limit?



Solution 1:[1]

The example above built an array inside an array

Array ( [limit] => 4 [skip] => 0 [sort] => Array ( [likes] => -1 ) [0] => Array ( [projection] => Array ( [_id] => 0 [likes] => 1 [data.name] => 1 [user] => 1 [data] => Array ( [$slice] => -1 ) ) ) ) 

which should have been

Array ( [limit] => 4 [skip] => 0 [sort] => Array ( [likes] => -1 ) [projection] => Array ( [_id] => 0 [likes] => 1 [data.name] => 1 [user] => 1 [data] => Array ( [$slice] => -1 ) ) )

Note the Array index

[0] => Array

Showing the projection inside an array, this was incorrect. The example in the question had mutltiple options in different array elements the mongodb library was only running the code from the first array element, thus skipping the second of either the limit or sort. A working mongodb search sort and limit:

    <?php
require 'vendor/autoload.php';
$conn = new MongoDB\Client('mongodb://172.17.0.3:27017');
$db = $conn->mydb;
$collection = $db->mycol;
$projection =  array("_id" => 0, "likes" => 1, "data.name" => 1, "user" => 1, 'data' => ['$slice' => -1]);
$sort = array('likes' => -1);
$options =  array('limit' => 4, 'skip' => 0, 'sort' => $sort, 'projection' => $projection);
print_r($options);
$cursor = $collection->find([], $options);

foreach($cursor as $document)
{
    var_dump ($document);
    echo "<br/><br/><br/>";
}
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mike