聚合和加入操作不起作用

Aggerate and join operation not working

我正在使用 mongo db php 1.6.8 驱动程序,我正在尝试执行如下代码行的聚合查询。

        public function fetchAdd()
        {
        $this->collection = $this->db->broadcastTbl;

        $query =  array('$or' => array(
        array('studentList'=>array('$exists' => false)),
        array('studentList'=>array('$eq' => null)),
        array('studentList'=>array('$eq' => ",")),
        array('studentList'=>array('$eq' => ""))
         )); 

        $c = $this->collection->find($query);

        $pipeline = array(
          array(
         '$lookup' => array(
         'from' => $this->db->userTbl,
         'localField' => 'uid',
         'foreignField' => 'user_id',
         'as' => 'username'
         )
        ),
          array(
         '$match' => array(
        'user_id' => array('$ne' => 'null')
         )
        ),
     );
     $out = $c->aggregate($pipeline);
    foreach ($out as $k => $srow)
    {                                            
        array_push($result, $srow);
    }   
      return json_encode($result);      
    }

我有

这样的数据

userTbl

   {
"_id" : ObjectId("5715e61d99fbad983700002e"),
"uid" : 23,
"username" : "MuneebZahoorMalik23",
"password" : "dummy#123",
"email_id" : "abc@gmail.com",
"creation_date" : "19/04/2016",
"role" : "student",
"is_enabled" : 1,
"student_id" : 23
 }
 {
"_id" : ObjectId("5715e6d999fbad9837000030"),
"uid" : 1,
"username" : "JunaidZahoorMalik24",
"password" : "dummy#123",
"email_id" : "abc@gmail.com",
"creation_date" : "19/04/2016",
"role" : "Admin",
"is_enabled" : 1
}

广播

    {"_id"{"$id":"58d8ea951d7859c81300002d"},"broadcast_id":71,"studentList":"5044","employeeList":"","mailTitle":"Hello","broadcastMessage":"Hello mellow","emailSent":"0"},
    {"_id":{"$id":"58dcdff61d78599c11000029"},"user_id":null,"broadcast_id":73,"studentList":"5042|5043|5044","employeeList":"","mailTitle":"Hello","broadcastMessage":"Jee","emailSent":"0","dateSent":"30\/03\/2017"},
    {"_id":{"$id":"58dce1971d7859f007000029"},"user_id":null,"broadcast_id":74,"studentList":"5045","employeeList":"","mailTitle":"Hello","broadcastMessage":"Jee","emailSent":"0","dateSent":"2\/2\/2015"},
    {"_id":{"$id":"58dce26f1d78599c1100002a"},"user_id":1,"broadcast_id":75,"studentList":"4|13","employeeList":"","mailTitle":"Hello","broadcastMessage":"Jee","emailSent":"0","dateSent":"30\/03\/2017"}

现在它会抛出错误信息。

调用未定义的方法 MongoCursor::aggregate()

我已经尝试了很多,但找不到任何执行连接操作的解决方案。

请帮忙!!!

请尝试以下功能。更改包括添加 $match 阶段作为第一个管道并在集合上使用 aggregate 函数。

public function fetchAdd()
        {
        $query =  array('$or' => array(
        array('studentList'=>array('$exists' => false)),
        array('studentList'=>array('$eq' => null)),
        array('studentList'=>array('$eq' => ",")),
        array('studentList'=>array('$eq' => ""))
         )); 

        $pipeline = array(
          array(
         '$match' => $query
        ),
          array(
         '$lookup' => array(
         'from' => $this->db->userTbl,
         'localField' => 'user_id',
         'foreignField' => 'uid',
         'as' => 'username'
         )
        ),
          array(
         '$match' => array(
        'user_id' => array('$ne' => 'null')
         )
        ),
     );
     $out = $this->db->broadcastTbl->aggregate($pipeline);
    foreach ($out as $k => $srow)
    {                                            
        array_push($result, $srow);
    }   
 return json_encode($result);    
}