MongoDB\Driver\Manager::executeCommand
(mongodb >=1.0.0)
MongoDB\Driver\Manager::executeCommand — 데이터베이스 명령 실행
설명
final public MongoDB\Driver\Manager::executeCommand(string $db
, MongoDB\Driver\Command $command
, array $options
= array()): MongoDB\Driver\Cursor
"readPreference"
옵션에 따라 서버를 선택하고 해당 서버에서 명령을 실행합니다. 기본적으로 MongoDB 연결 URI의 읽기 기본 설정이 사용됩니다.
이 메서드는 명령에 특별한 논리를 적용하지 않습니다. 이 메서드는 명령 문서에 통합될 "readConcern"
및 "writeConcern"
옵션을 허용하지만 이러한 옵션은 기본적으로 MongoDB
Connection URI의 해당 값으로 설정되지 않으며 MongoDB 서버 버전도 고려되지 않습니다. 따라서 사용자는 가능한 경우 특정 읽기 및/또는 쓰기 명령 메서드를 사용하는 것이 좋습니다.
매개변수
db
(string)- 명령을 실행할 데이터베이스의 이름입니다.
command
(MongoDB\Driver\Command)- 실행할 명령입니다.
options
-
options
Option Type 설명 readConcern MongoDB\Driver\ReadConcern 작업에 적용할 읽기 관심사입니다. 이 옵션은 MongoDB 3.2 이상에서 사용할 수 있으며 이전 서버 버전에 대해 지정된 경우 실행 시 예외가 발생합니다.
readPreference MongoDB\Driver\ReadPreference 작업을 위한 서버를 선택하는 데 사용할 읽기 기본 설정입니다. session MongoDB\Driver\Session 작업과 연결할 세션입니다. writeConcern MongoDB\Driver\WriteConcern 작업에 적용할 쓰기 관심사입니다. 경고 트랜잭션이 진행 중인
"session"
을 사용하는 경우"readConcern"
또는"writeConcern"
옵션을 지정할 수 없습니다. 그러면 MongoDB\Driver\Exception\InvalidArgumentException이 발생합니다. 대신 MongoDB\Driver\Session::startTransaction()으로 트랜잭션을 생성할 때 이 두 가지 옵션을 설정해야 합니다.
반환 값
성공하면 MongoDB\Driver\Cursor를 반환합니다.
오류/예외
-
"session"
옵션이"readConcern"
또는"writeConcern"
옵션과 함께 연결된 트랜잭션과 함께 사용되는 경우 MongoDB\Driver\Exception\InvalidArgumentException을 던집니다. -
"session"
옵션이 승인되지 않은 쓰기 문제와 함께 사용되는 경우 MongoDB\Driver\Exception\InvalidArgumentException이 발생합니다. - 인수 구문 분석 오류에 대해 MongoDB\Driver\Exception\InvalidArgumentException을 던집니다.
- (인증 이외의 이유로) 서버 연결이 실패하면 MongoDB\Driver\Exception\ConnectionException을 던집니다.
- Throws 인증이 필요하고 실패하면 MongoDB\Driver\Exception\AuthenticationException을 던집니다.
- 다른 오류(예: 잘못된 명령, 보조에 쓰기 명령 실행)에서 MongoDB\Driver\Exception\RuntimeException을 발생시킵니다.
변경 로그
버전 | 설명 |
---|---|
PECL mongodb 1.4.4 | "session" 옵션이 승인되지 않은 쓰기 문제와 함께 사용되면 MongoDB\Driver\Exception\InvalidArgumentException이 발생합니다. |
PECL mongodb 1.4.0 | 세 번째 매개변수는 이제 options 배열입니다. 이전 버전과의 호환성을 위해 이 매개변수는 여전히 MongoDB\Driver\ReadPreference 개체를 허용합니다. |
Examples
예제 #1 단일 결과 문서를 반환하는 명령이 있는 MongoDB\Driver\Manager::executeCommand()
<?php
$manager = 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) {
echo $e->getMessage(), "\n";
exit;
}
/* The ping command returns a single result document, so we need to access the
* first result in the cursor. */
$response = $cursor->toArray()[0];
var_dump($response);
?>
위의 예는 다음을 출력합니다.
array(1) { ["ok"]=> float(1) }
예제 #2 MongoDB\Driver\Manager::executeCommand() 커서를 반환하는 명령
<?php
$manager = 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']);
$manager->executeBulkWrite('db.collection', $bulk);
$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) {
var_dump($document);
}
?>
위의 예는 다음을 출력합니다.
object(stdClass)#6 (2) { ["_id"]=> string(3) "bar" ["sum"]=> int(10) } object(stdClass)#7 (2) { ["_id"]=> string(3) "foo" ["sum"]=> int(2) }
예제 #3 명령의 실행 시간 제한
MongoDB\Driver\Command 문서에서 "maxTimeMS"
값을 지정하여 명령 실행 시간을 제한할 수 있습니다. 이 시간 제한은 서버 측에서 적용되며 네트워크 대기 시간을 고려하지 않습니다. 자세한 내용은 MongoDB 설명서에서 » Terminate Running Operations를 참조하세요.
<?php
$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$command = new MongoDB\Driver\Command([
'count' => 'collection',
'query' => ['x' => ['$gt' => 1]],
'maxTimeMS' => 1000,
]);
$cursor = $manager->executeCommand('db', $command);
var_dump($cursor->toArray()[0]);
?>
서버에서 1초의 실행 시간 후에도 명령이 완료되지 않으면 MongoDB\Driver\Exception\ExecutionTimeoutException이 발생합니다.
노트
참고: 보조 readPreference
가 사용되는 경우 보조에서 command
을 실행할 수 있는지 확인하는 것은 호출자의 책임입니다. 드라이버는 유효성 검사를 수행하지 않습니다.
기타
- MongoDB\Driver\Command
- MongoDB\Driver\Cursor
- MongoDB\Driver\Manager::executeReadCommand() - 다음을 읽는 데이터베이스 명령을 실행합니다.
- MongoDB\Driver\Manager::executeReadWriteCommand() - 읽고 쓰는 데이터베이스 명령 실행
- MongoDB\Driver\Manager::executeWriteCommand() - 다음을 작성하는 데이터베이스 명령을 실행합니다.
- MongoDB\Driver\Server::executeCommand() - 이 서버에서 데이터베이스 명령 실행