PHP 8.4.0 RC3 available for testing

La classe MongoDB\Driver\ServerApi

(mongodb >=1.10.0)

Introduction

Synopsis de la classe

final class MongoDB\Driver\ServerApi implements MongoDB\BSON\Serializable, Serializable {
/* Constantes */
/* Méthodes */
final public bsonSerialize(): stdClass
final public __construct(string $version, ?bool $strict = null, ?bool $deprecationErrors = null)
final public serialize(): string
final public unserialize(string $data): void
}

Constantes pré-définies

MongoDB\Driver\ServerApi::V1

Version 1 de l'API serveur.

Exemples

Exemple #1 Déclarer une version d'API sur un gestionnaire

<?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;
}

/* La commande buildInfo retourne un document unique, nous devons donc accéder
* au premier résultat du curseur. */
$buildInfo = $cursor->toArray()[0];

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

?>

L'exemple ci-dessus va afficher :

4.9.0-alpha7-49-gb968ca0

Exemple #2 Déclare une version d'API stricte sur un gestionnaire

L'exemple suivant définit le drapeau strict, qui indique au serveur de rejeter toute commande qui ne fait pas partie de la version d'API déclarée. Cela entraîne une erreur lors de l'exécution de la commande 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";

?>

L'exemple ci-dessus va afficher :

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

Sommaire

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top