update : 2015.11.03
php.shukuma.com

검색:
 
 
Execute a MongoDB database command

MongoDB\Driver\Manager::executeCommand

(mongodb >=0.2.0)

MongoDB\Driver\Manager::executeCommandExecute a MongoDB database command

설명

final public MongoDB\Driver\Cursor MongoDB\Driver\Manager::executeCommand ( string $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

Optionally, a MongoDB\Driver\ReadPreference to route the command to. If none given, defaults to the Read Preferences set by the MongoDB Connection URI.

반환값

Returns MongoDB\Driver\Cursor on success, throws exception (instanceof MongoDB\Driver\Exception) on failure.

오류/예외

  • Throws MongoDB\Driver\AuthenticationException if authentication is needed and fails
  • Throws MongoDB\Driver\ConnectionException if connection to the server fails for other then authentication reasons
  • Throws MongoDB\Driver\DuplicateKeyException if a write causes Duplicate Key error
  • Throws MongoDB\Driver\WriteException on Write Error
  • Throws MongoDB\Driver\WriteConcernException on Write Concern failure
  • Throws MongoDB\Driver\RuntimeException on other errors (invalid command, command arguments, ...)

예제

Example #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)
}

주의

Note:

For write commands, MongoDB\Driver\WriteConcern is included in the command document itself.

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.