管道阶段规范对象必须仅包含一个带有php mongo聚合的字段 [英] A pipeline stage specification object must contain exactly one field with php mongo aggregate

查看:55
本文介绍了管道阶段规范对象必须仅包含一个带有php mongo聚合的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将aggregate与项目,匹配项和排序项一起使用,但出现了异常(MongoResultException准确地说)

I am trying to use aggregate with with project, match and sort but i am getting an exception (MongoResultException to be exact) saying

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

当我不使用排序和限制,但为此需要它们时,它工作正常.我之所以不使用find()的原因是因为我在某处读到它可以提高性能. 请帮助

It works fine when I did not use sort and limit but I need them for this. The reason I am not using find() 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;

推荐答案

问题似乎出在您的$sort作业上.你有

It looks like the problem is your $sort assignment. You have

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

实际上并没有给出$sort阶段规范.不应该是:

which doesn't actually give the $sort stage specification. Shouldn't it be:

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

这篇关于管道阶段规范对象必须仅包含一个带有php mongo聚合的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆