(mongodb >=1.10.0)
MongoDB\Driver\ServerApi::V1Version 1 de l'API serveur.
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