MongoDB\Driver\Command::__construct
(mongodb >=1.0.0)
MongoDB\Driver\Command::__construct — 새 명령 생성
설명
final public MongoDB\Driver\Command::__construct(array|object $document
, array $commandOptions
= ?)
데이터베이스 명령을 나타내는 변경할 수 없는 값 개체인 새 MongoDB\Driver\Command를 구성합니다. 그런 다음 MongoDB\Driver\Manager::executeCommand()를 사용하여 명령을 실행할 수 있습니다.
명령 이름과 해당 옵션을 포함하는 완전한 명령 문서는 document
매개변수에 표현되어야 합니다. commandOptions
매개변수는 명령 실행 및 결과 MongoDB\Driver\Cursor와 관련된 옵션을 지정하는 데만 사용됩니다.
매개변수
document
- 서버로 보내질 완전한 명령 문서.
commandOptions
-
참고: 이 매개변수를 사용하여 MongoDB 매뉴얼의 명령 참조에 설명된 옵션을 지정하지 마십시오. 이 매개변수는 아래에 명시적으로 나열된 옵션에만 사용해야 합니다.
commandOptions
Option Type 설명 maxAwaitTimeMS int 데이터를 사용할 수 없는 경우 서버가 getMore 작업을 차단하는 시간 제한(밀리초)을 나타내는 양의 정수입니다. 이 옵션은 tailable 커서를 반환하는 명령과 함께 사용해야 합니다(예: » Change Streams).
오류/예외
- 인수 구문 분석 오류에 대해 MongoDB\Driver\Exception\InvalidArgumentException을 던집니다.
변경 로그
버전 | 설명 |
---|---|
PECL mongodb 1.4.0 | "maxAwaitTimeMS" 옵션을 지원하는 두 번째 commandOptions 인수를 추가했습니다. |
Examples
예제 #1 MongoDB\Driver\Command::__construct() 예제
<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$command = new MongoDB\Driver\Command(array("buildinfo" => 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(13) { ["version"]=> string(14) "2.8.0-rc2-pre-" ["gitVersion"]=> string(62) "b743d7158f7642f4da6b7eac8320374b3b88dc2e modules: subscription" ["OpenSSLVersion"]=> string(25) "OpenSSL 1.0.1f 6 Jan 2014" ["sysInfo"]=> string(104) "Linux infant 3.16.0-24-generic #32-Ubuntu SMP Tue Oct 28 13:07:32 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49" ["loaderFlags"]=> string(91) "-fPIC -pthread -Wl,-z,now -rdynamic -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,-E" ["compilerFlags"]=> string(301) "-Wnon-virtual-dtor -Woverloaded-virtual -std=c++11 -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -pipe -Werror -O3 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-but-set-variable -fno-builtin-memcmp -std=c99" ["allocator"]=> string(8) "tcmalloc" ["versionArray"]=> array(4) { [0]=> int(2) [1]=> int(8) [2]=> int(0) [3]=> int(-8) } ["javascriptEngine"]=> string(2) "V8" ["bits"]=> int(64) ["debug"]=> bool(false) ["maxBsonObjectSize"]=> int(16777216) ["ok"]=> float(1) }
예제 #2 MongoDB\Driver\Command::__construct() 예제
명령은 서버에 보내기 위해 만드는 일반 구조의 일부로 옵션도 받아들일 수 있습니다. 예를 들어, maxTimeMS 옵션은 특정 명령이 서버에서 실행될 수 있는 시간을 제한하기 위해 대부분의 명령과 함께 전달될 수 있습니다.
<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$command = new MongoDB\Driver\Command(
array(
"distinct" => "beer",
"key" => "beer_name",
"maxTimeMS" => 10,
)
);
try {
$cursor = $manager->executeCommand("beerdb", $command);
$response = $cursor->toArray()[0];
} catch(MongoDB\Driver\Exception\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
var_dump($response);
?>
위의 예는 다음과 유사한 결과를 출력합니다.
operation exceeded time limit
기타
- MongoDB\Driver\Manager::executeCommand() - 데이터베이스 명령 실행
- MongoDB\Driver\Cursor