MongoCollection::aggregate() is undefined in Herok

2019-09-11 07:18发布

问题:

I'm getting the following error when using MongoDB's aggregate() function in a PHP code. This code perfectly works on my local setup which is running MongoDB 2.2.3

PHP Fatal error: Call to undefined method MongoCollection::aggregate() in /app/www/page.php on line 52, referer: http://referrer.url

Code

foreach($cats as $key=>$val){
    $cats2[$val['lable']] = $myCollection->aggregate( array(
                                array('$match' => array('user_id' => $user_id )),
                                array('$unwind' =>"\$data"),
                                array('$match' => array('data.category'=> $val['category'])),
                                array('$project' => array('name'=> "\$data.name", 'id'=>"\$data.id")),
                                array('$group' => array('_id'=>'$id', 'name'=> array('$first' =>'$name'))),
                                array('$limit' => 12)
                                    ));
}

Environment

  • PHP on Heroku
  • MongoDB 2.2.4 with MongoHQ Add-on

回答1:

You have to look into your phpinfo()

If in the version you see something less than 1.3.0, then it will not work.

Go to mongo driver page download and add appropriate driver. Restart the server and it will work.