Navigation

MongoDB\Database::command()

Definition

MongoDB\Database::command

Execute a command on the database.

function command($command, array $options = []): MongoDB\Driver\Cursor

This method has the following parameters:

Parameter Type Description
$command array|object The database command document.
$options array Optional. An array specifying the desired options.

The $options parameter supports the following options:

Option Type Description
readPreference MongoDB\Driver\ReadPreference Optional. Read preference to use for the operation. Defaults to the database’s read preference.
typeMap array Optional. The type map to apply to cursors, which determines how BSON documents are converted to PHP values. Defaults to the database’s type map.

Return Values

A MongoDB\Driver\Cursor object.

Errors/Exceptions

MongoDB\Exception\InvalidArgumentException for errors related to the parsing of parameters or options.

MongoDB\Driver\Exception\RuntimeException for other errors at the driver level (e.g. connection errors).

Example

The following example executes an isMaster command, which returns a cursor with a single result document:

<?php

$database = (new MongoDB\Client)->test;

$cursor = $database->command(['isMaster' => 1]);

var_dump($c->toArray()[0]);

The output would resemble:

object(MongoDB\Model\BSONDocument)#11 (1) {
  ["storage":"ArrayObject":private]=>
  array(8) {
    ["ismaster"]=>
    bool(true)
    ["maxBsonObjectSize"]=>
    int(16777216)
    ["maxMessageSizeBytes"]=>
    int(48000000)
    ["maxWriteBatchSize"]=>
    int(1000)
    ["localTime"]=>
    object(MongoDB\BSON\UTCDateTime)#3 (1) {
      ["milliseconds"]=>
      string(13) "1477608046464"
    }
    ["maxWireVersion"]=>
    int(4)
    ["minWireVersion"]=>
    int(0)
    ["ok"]=>
    float(1)
  }
}

The following example executes a listCollections command, which returns a cursor with multiple result documents:

<?php

$database = (new MongoDB\Client)->test;

$cursor = $database->command(['isMaster' => 1]);

var_dump($c->toArray());

The output would resemble:

array(3) {
  [0]=>
  object(MongoDB\Model\BSONDocument)#11 (1) {
    ["storage":"ArrayObject":private]=>
    array(2) {
      ["name"]=>
      string(11) "restaurants"
      ["options"]=>
      object(MongoDB\Model\BSONDocument)#3 (1) {
        ["storage":"ArrayObject":private]=>
        array(0) {
        }
      }
    }
  }
  [1]=>
  object(MongoDB\Model\BSONDocument)#13 (1) {
    ["storage":"ArrayObject":private]=>
    array(2) {
      ["name"]=>
      string(5) "users"
      ["options"]=>
      object(MongoDB\Model\BSONDocument)#12 (1) {
        ["storage":"ArrayObject":private]=>
        array(0) {
        }
      }
    }
  }
  [2]=>
  object(MongoDB\Model\BSONDocument)#15 (1) {
    ["storage":"ArrayObject":private]=>
    array(2) {
      ["name"]=>
      string(6) "restos"
      ["options"]=>
      object(MongoDB\Model\BSONDocument)#14 (1) {
        ["storage":"ArrayObject":private]=>
        array(0) {
        }
      }
    }
  }
}