La classe MongoDB\Driver\Manager

(mongodb >=1.0.0)

Introduction

Le MongoDB\Driver\Manager est le point d'entrée principal de l'extension. Il est responsable de maintenir les connexions à MongoDB (qu'il s'agisse d'un serveur autonome, d'un ensemble de réplicas ou d'un cluster partagé).

Aucune connexion Ă  MongoDB n'est Ă©tablie lors de l'instanciation du Manager. Cela signifie que la MongoDB\Driver\Manager peut toujours ĂȘtre construit, mĂȘme si un ou plusieurs serveurs MongoDB sont hors service.

Toute Ă©criture ou requĂȘte peut lancer des exceptions de connexion car les connexions sont créées de maniĂšre paresseuse. Un serveur MongoDB peut Ă©galement devenir indisponible pendant la durĂ©e de vie du script. Il est donc important que toutes les actions sur le Manager soient enveloppĂ©es dans des instructions try/catch.

Synopsis de la classe

final class MongoDB\Driver\Manager {
/* Méthodes */
final public function addSubscriber(MongoDB\Driver\Monitoring\Subscriber $subscriber): void
final public function __construct(?string $uri = null, ?array $uriOptions = null, ?array $driverOptions = null)
final public function executeBulkWrite(string $namespace, MongoDB\Driver\BulkWrite $bulk, ?array $options = null): MongoDB\Driver\WriteResult
final public function executeCommand(string $db, MongoDB\Driver\Command $command, ?array $options = null): MongoDB\Driver\Cursor
final public function executeQuery(string $namespace, MongoDB\Driver\Query $query, ?array $options = null): MongoDB\Driver\Cursor
final public function executeReadCommand(string $db, MongoDB\Driver\Command $command, ?array $options = null): MongoDB\Driver\Cursor
final public function executeReadWriteCommand(string $db, MongoDB\Driver\Command $command, ?array $options = null): MongoDB\Driver\Cursor
final public function executeWriteCommand(string $db, MongoDB\Driver\Command $command, ?array $options = null): MongoDB\Driver\Cursor
final public function getEncryptedFieldsMap(): array|object|null
final public function getServers(): array
final public function removeSubscriber(MongoDB\Driver\Monitoring\Subscriber $subscriber): void
final public function selectServer(?MongoDB\Driver\ReadPreference $readPreference = null): MongoDB\Driver\Server
final public function startSession(?array $options = null): MongoDB\Driver\Session
}

Exemples

Exemple #1 Usage basique de MongoDB\Driver\Manager::__construct()

var_dump()er un MongoDB\Driver\Manager affichera divers dĂ©tails sur le Manager qui ne sont pas normalement exposĂ©s. Cela peut ĂȘtre utile pour dĂ©boguer comment le pilote voit la configuration MongoDB, et quelles options sont utilisĂ©es.

<?php

$manager
= new MongoDB\Driver\Manager('mongodb://localhost:27017');
var_dump($manager);

?>

Résultat de l'exemple ci-dessus est similaire à :

object(MongoDB\Driver\Manager)#1 (2) {
  ["uri"]=>
  string(26) "mongodb://127.0.0.1:27017/"
  ["cluster"]=>
  array(0) {
  }
}

Sommaire

add a note

User Contributed Notes 1 note

up
9
mike at eastghost dot com ¶
7 years ago
According to Mongo, this (i.e., MongoDB\Driver\Manager) is an "entry point" for the extension:

"This class serves as an entry point for the MongoDB PHP Library. It is the preferred class for connecting to a MongoDB server or cluster of servers and acts as a gateway for accessing individual databases and collections. MongoDB\Client is analogous to the driver’s MongoDB\Driver\Manager class, which it composes."

copied from here: https://docs.mongodb.com/php-library/master/reference/class/MongoDBClient/

However, any comparison of the "mongodb" docs here on php.net versus the "mongodb driver" docs on mongo's site shows dramatic and ever-changing differences.