The pipeline stage specification object must contain exactly one field with php mongo aggregate

I try to use aggregate with with project, match and sort, but I get an exception ( MongoResultException to be precise), saying

exception: A pipeline stage specification object must contain exactly one field.

It works great when I did not use sorting and restriction, but for this I need. The reason I don't use find() is because I read somewhere that it can increase performance. Please, help

 $query = array(.... //An actual query that works with find() $collection = $this->db->CollectionName; $project = array( '$project' => array( 'Field1' => 1, 'Field2'=> 1, 'Field3'=> 1, 'Field4' => 1 ) ); $match = array( '$match'=>$query); $sort = array('Field3' => -1, 'Field4'=>-1); $limit = array('$limit' => 100); $result = $collection->aggregate(array($match,$project,$sort,$limit)); return $result; 
+5
source share
1 answer

It seems like the problem is that you are specifying $ sort. You

  $sort = array('Field3' => -1, 'Field4'=>-1); 

which does not actually give the $ sort stage specification. Must not be:

  $sort = array('$sort' => array( 'Field3' => -1, 'Field4'=>-1 ) ); 
+4
source

All Articles