GoLang MongoDB\Driver\Manager::executeCommand

request it (300)
GoLang replacement for PHP's MongoDB\Driver\Manager::executeCommand [edit | history]

Do you know a GoLang replacement for PHP's MongoDB\Driver\Manager::executeCommand? Write it!

PHP MongoDB\Driver\Manager::executeCommand

PHP original manual for MongoDB\Driver\Manager::executeCommand [ show | php.net ]


(mongodb >=1.0.0)

MongoDB\Driver\Manager::executeCommandExecute a database command


final public MongoDB\Driver\Cursor MongoDB\Driver\Manager::executeCommand ( string $db , MongoDB\Driver\Command $command [, array $options = array() ] )

Selects a server according to the "readPreference" option and executes the command on that server. By default, the read preference from the MongoDB Connection URI will be used.

This method applies no special logic to the command. Although this method accepts "readConcern" and "writeConcern" options, which will be incorporated into the command document, those options will not default to corresponding values from the MongoDB Connection URI nor will the MongoDB server version be taken into account. Users are therefore encouraged to use specific read and/or write command methods if possible.


db (string)

The name of the database on which to execute the command.

command (MongoDB\Driver\Command)

The command to execute.


Option Type Description
readConcern MongoDB\Driver\ReadConcern

A read concern to apply to the operation.

This option is available in MongoDB 3.2+ and will result in an exception at execution time if specified for an older server version.

readPreference MongoDB\Driver\ReadPreference

A read preference to use for selecting a server for the operation.

session MongoDB\Driver\Session

A session to associate with the operation.

writeConcern MongoDB\Driver\WriteConcern

A write concern to apply to the operation.

Return Values

Returns MongoDB\Driver\Cursor on success.



Version Description
1.4.0 The third parameter is now an options array. For backwards compatibility, this paramater will still accept a MongoDB\Driver\ReadPreference object.


Example #1 MongoDB\Driver\Manager::executeCommand() with a command returning a single result document


= new MongoDB\Driver\Manager('mongodb://localhost:27017');
$command = new MongoDB\Driver\Command(['ping' => 1]);

try {
$cursor $manager->executeCommand('admin'$command);
} catch(
MongoDB\Driver\Exception $e) {
$e->getMessage(), "\n";

/* The ping command returns a single result document, so we need to access the
 * first result in the cursor. */
$response $cursor->toArray()[0];



The above example will output:

array(1) {

Example #2 MongoDB\Driver\Manager::executeCommand() with a command returning a cursor


= new MongoDB\Driver\Manager("mongodb://localhost:27017");

$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1'y' => 'foo']);
$bulk->insert(['x' => 2'y' => 'bar']);
$bulk->insert(['x' => 3'y' => 'bar']);

$command = new MongoDB\Driver\Command([
'aggregate' => 'collection',
'pipeline' => [
'$group' => ['_id' => '$y''sum' => ['$sum' => '$x']]],
'cursor' => new stdClass,
$cursor $manager->executeCommand('db'$command);

/* The aggregate command can optionally return its results in a cursor instead
 * of a single result document. In this case, we can iterate on the cursor
 * directly to access those results. */
foreach ($cursor as $document) {


The above example will output:

object(stdClass)#6 (2) {
  string(3) "bar"
object(stdClass)#7 (2) {
  string(3) "foo"


Note: If a secondary readPreference is used, it is the caller's responsibility to ensure that the command can be executed on a secondary. No validation is done by the driver.

See Also