MongoDB\Driver\ServerApi 클래스

(mongodb >=1.10.0)


소개


클래스 개요

final class MongoDB\Driver\ServerApi implements MongoDB\BSON\Serializable, Serializable {
  /* Constants */
  const string MongoDB\Driver\ServerAPI::V1 = "1";
  /* Methods */
  final public bsonSerialize(): object
  final public __construct(string $version, bool $strict = null, bool $deprecationErrors = null)
  final public serialize(): string
  final public unserialize(string $serialized): void
}
                

미리 정의된 상수

MongoDB\Driver\ServerApi::V1
서버 API 버전 1.

Examples

예제 #1 관리자에서 API 버전 선언

                  
<?php

use MongoDB\Driver\Manager;
use MongoDB\Driver\ServerApi;

$v1 = new ServerApi(ServerApi::v1);
$manager = new Manager('mongodb://localhost:27017', [], ['serverApi' => $v1]);

$command = new MongoDB\Driver\Command(['buildInfo' => 1]);

try {
    $cursor = $manager->executeCommand('admin', $command);
} catch(MongoDB\Driver\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

/* The buildInfo command returns a single result document, so we need to access
 * the first result in the cursor. */
$buildInfo = $cursor->toArray()[0];

echo $buildInfo->version, "\n";

?>
                  
                

위의 예는 다음과 유사한 결과를 출력합니다.

4.9.0-alpha7-49-gb968ca0
                

예제 #2 관리자에서 엄격한 API 버전 선언

다음 예에서는 선언된 API 버전의 일부가 아닌 명령을 거부하도록 서버에 지시하는 strict 플래그를 설정합니다. 이로 인해 buildInfo 명령을 실행할 때 오류가 발생합니다.

                  
<?php

use MongoDB\Driver\Manager;
use MongoDB\Driver\ServerApi;

$v1 = new ServerApi(ServerApi::v1, true);
$manager = new Manager('mongodb://localhost:27017', [], ['serverApi' => $v1]);

$command = new MongoDB\Driver\Command(['buildInfo' => 1]);

try {
    $cursor = $manager->executeCommand('admin', $command);
} catch(MongoDB\Driver\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

/* The buildInfo command returns a single result document, so we need to access
 * the first result in the cursor. */
$buildInfo = $cursor->toArray()[0];

echo $buildInfo->version, "\n";

?>
                  
                

위의 예는 다음과 유사한 결과를 출력합니다.

Provided apiStrict:true, but the command buildInfo is not in API Version 1
                

목차