【发布时间】:2021-11-09 18:27:08
【问题描述】:
我在 MongoDB 中使用这个命令,它返回正确的数据:
db.getCollection('school').aggregate([{ $group: { _id: "$initials", total: { $sum: "$total" }}}, { $sort : { _id : 1}}])
但是当我用 PHP 编写该命令时,我得到了错误。我正在尝试:
$mng = new MongoDB\Driver\Manager();
$command = new MongoDB\Driver\Command(['aggregate' => 'school', 'pipeline' => [ '$group' => ['_id' => '$initials', 'count' => [ '$sum' => '$total']], 'cursor' => new stdClass ]]);
$cursor = $mng->executeCommand('school', $command);
var_dump($cursor->toArray()[0]);
有人可以帮我吗?
【问题讨论】:
-
错误是什么?
-
PHP 致命错误:未捕获的 MongoDB\Driver\Exception\CommandException:'pipeline' 选项必须在 /var/www/html/mongodbtest.php:45 中指定为数组
-
试试
$command = new MongoDB\Driver\Command(['aggregate' => 'school', 'pipeline' => [[ '$group' => ['_id' => '$initials', 'count' => [ '$sum' => '$total']]], 'cursor' => new stdClass ]]);(管道需要“命令”数组,每个都应该是数组)