(mongodb >=0.2.0)
MongoDB\Driver\Manager::executeCommand — Execute a MongoDB database command
$db
, MongoDB\Driver\Command $command
[, MongoDB\Driver\ReadPreference $readPreference
] )
Executes command on a MongoDB server matching readPreference.
dbThe name of the database on which to execute the command.
commandThe command document.
readPreferenceオプションで、コマンドを渡す MongoDB\Driver\ReadPreference を指定します。省略した場合は、MongoDB Connection URI で設定したデフォルトの優先読み込みを使います。
Returns MongoDB\Driver\Cursor on success, 失敗した場合に例外 (MongoDB\Driver\Exception のインスタンス) をスローします.
例1 MongoDB\Driver\Manager::executeCommand() example
<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$command = new MongoDB\Driver\Command(array("ping" => 1));
try {
$cursor = $manager->executeCommand("admin", $command);
$response = $cursor->toArray()[0];
} catch(MongoDB\Driver\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
var_dump($response);
?>
上の例の出力は、 たとえば以下のようになります。
array(1) {
["ok"]=>
float(1)
}
注意:
For write commands, MongoDB\Driver\WriteConcern is included in the
commanddocument itself.
注意:
If a secondary
readPreferenceis used, it is the caller's responsibility to ensure that thecommandcan be executed on a secondary. No validation is done by the driver.