Aggerate和join操作不起作用

时间:2017-04-05 05:50:07

标签: php mongodb mongodb-php

我正在使用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
}

broadcastTbl

    {"_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()

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

请帮助!!!

1 个答案:

答案 0 :(得分:1)

请尝试以下功能。更改包括将$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);    
}