如何使用PHP中的特定字段对mongodb中的记录进行分组

时间:2019-01-19 11:19:00

标签: mongodb

我是mongodb中的新手,我正在尝试通过email_address字段对分组进行计数,但这给了我这个错误,这是我的代码,任何人都可以帮助我,如何解决此错误?

查询

    $m = new MongoDB\Client("mongodb://localhost:27017");

     $db = $m->mailchimp;   
     $collection = $db->users;
      $aggregate[] = [
    '$group' => [
        '_id' => [
            'email_address' => '',
        ],
    ],
   ]; 
   $data=$collection1->aggregate($aggregate);

错误:

PHP Fatal error:  Uncaught MongoDB\Driver\Exception\CommandException: a group specification must include an _id in /var/www/html/vendor/mongodb/mongodb/src/Operation/Aggregate.php:263
Stack trace:
#0 /var/www/html/vendor/mongodb/mongodb/src/Operation/Aggregate.php(263): MongoDB\Driver\Server->executeReadCommand('mailchimp', Object(MongoDB\Driver\Command), Array)
#1 /var/www/html/vendor/mongodb/mongodb/src/Collection.php(223): MongoDB\Operation\Aggregate->execute(Object(MongoDB\Driver\Server))
#2 /var/www/html/checklog.php(59): MongoDB\Collection->aggregate(Array)
#3 {main}
  thrown in /var/www/html/vendor/mongodb/mongodb/src/Operation/Aggregate.php on line 263
ubuntu@marketer-box:/var/www/html$ php checklog.php
PHP Notice:  Undefined variable: collection1 in /var/www/html/checklog.php on line 58
PHP Fatal error:  Uncaught Error: Call to a member function aggregate() on null in /var/www/html/checklog.php:58

0 个答案:

没有答案