如何获取mongodb聚合函数中的所有字段?

时间:2017-05-09 06:56:19

标签: mongodb mongodb-query aggregation-framework mongodb-php

我已经在下面的代码行中了解了

public function BasicSearchStudents($input)
{
    $this->page =1;
    $page=(int)$this->page;
    $skip=($page - 1)*12;
    $param = preg_replace('!\s+!', ' ', $input);
    $arg = trim($param);

    $output= $this->db->studentTbl->aggregate(
          array(
                array(
                        '$project' => array(
                          'fullname' => array('$concat' => array('$first_name',  ' ',  '$middle_name', ' ', '$last_name')),
                          'firstmiddle' => array('$concat' => array('$first_name',  ' ',  '$middle_name')),
                          'firstlast' => array('$concat' => array('$first_name',  ' ',  '$last_name')),
                          'first_name' => 1,
                          'middle_name' =>1,
                          'last_name' =>1,
                          'roll_no' =>1,
                          'registration_temp_perm_no' =>1,
                          'email' =>1,
                          'guardian_name' =>1,
                          'phone1' =>1,
                          'phone2' =>1,
                          'dob' =>1,
                          'student_id' =>1,
                          'is_active' =>1,
                          'has_finished' =>1
                      )
                    ),
                 array(
                   '$match' =>
                         array('$or' => 
                            array(
                            array("fullname" => new MongoRegex("/$arg/i")),
                            array("first_name" => new MongoRegex("/$arg/i")),
                            array("middle_name" => new MongoRegex("/$arg/i")),
                            array("last_name" => new MongoRegex("/$arg/i")),
                            array("firstmiddle" => new MongoRegex("/$arg/i")),
                            array("firstlast" => new MongoRegex("/$arg/i")),
                            array("registration_temp_perm_no" => new MongoRegex("/$arg/i")),
                            array("roll_no" => new MongoRegex("/$arg/i")),
                            array("email" => new MongoRegex("/$arg/i")),
                            array("guardian_name" => new MongoRegex("/$arg/i")),
                            array("phone1" => new MongoRegex("/$arg/i")),
                            array("dob" => new MongoRegex("/$arg/i"))
                              )
                            )
                          ),
                    array(
                          'studentresult' => array(
                          '$push' => '$$ROOT'
                          )
                         ), 
                         array('$limit' => 22),
                         array('$skip' =>$skip),
                        )
                     );

           $cursor = $output["studentresult"];
    }

我试图通过使用root匹配操作来获取学生集合中使用的所有字段。它抛出错误“Uncaught exception'MongoResultException',消息'localhost:27017:无法识别的管道阶段名称:'结果''”。 请帮帮我。

0 个答案:

没有答案