php mongodb 手册,PHP: MongoDB\Driver\Command - Manual PHP中文手册 PHP中国镜像 php 国内镜像...

燕正卿
2023-12-01

0) Read the official MongoDB documentation for understand what db commands you can use and what parameters they requires - https://docs.mongodb.com/manual/reference/command/

1) Wrong:

$cmd = new \MongoDB\Driver\Command([

'aggregate' => 'collection',

'pipeline' => ['$group' => ['_id' => null, 'count' => ['$sum' => '$total']]]

]);

because pipeline is array of objects in json words (index array of associative arrays in php words) - pipeline: [ {}, ... ]

What does it mean? It means that  'pipeline' must be like this:

[

['$group' => ['_id' => null, 'count' => ['$sum' => '$total']]],    // this is {}

['$match' => [...]],    // and this

...

[...]    // and all of that

]

Just see https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/

2) The first pair in associative array for __construct's argument $document (if it is an array) must be a command name (e. g. 'count' => 'collectionName' or 'findAndModify' => 'collectionName'). I found this out experimentally, but you can examine source code  https://github.com/mongodb/mongo-php-driver/blob/master/src/MongoDB/Command.c for understand why it happens.

 类似资料: