(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
.
db
The name of the database on which to execute the command.
command
The command document.
readPreference
Opcionalmente, un MongoDB\Driver\ReadPreference a donde redireccionar el comando. Si no se proporciona, se empleará el valor de la Preferencia de lectura establcecida por el URI de conexión de MongoDB.
Returns MongoDB\Driver\Cursor on success, lanza una excepción (instanceof MongoDB\Driver\Exception) en caso de fallo.
Ejemplo #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);
?>
El resultado del ejemplo sería algo similar a:
array(1) { ["ok"]=> float(1) }
Nota:
For write commands, MongoDB\Driver\WriteConcern is included in the
command
document itself.
Nota:
If a secondary
readPreference
is used, it is the caller's responsibility to ensure that thecommand
can be executed on a secondary. No validation is done by the driver.