A classe MongoDB\Driver\Manager

(mongodb >=1.0.0)

Introdução

A classe MongoDB\Driver\Manager é o principal ponto de entrada para a extensão. É responsável por manter conexões com o MongoDB (seja ele servidor independente, conjunto de réplicas ou cluster fragmentado).

Nenhuma conexão com o MongoDB é feita ao instanciar a classe. Isso significa que o objeto MongoDB\Driver\Manager sempre pode ser construído, mesmo que um ou mais servidores MongoDB estejam inativos.

Qualquer escrita ou consulta pode lançar exceções de conexão, pois as conexões são criadas lentamente. Um servidor MongoDB também pode ficar indisponível durante a vida útil do script. Portanto, é importante que todas as ações no objeto Manager sejam agrupadas em instruções try/catch.

Resumo da classe

final class MongoDB\Driver\Manager {
/* Métodos */
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
}

Exemplos

Exemplo #1 Exemplo básico de MongoDB\Driver\Manager::__construct()

A função var_dump() executada em um objeto MongoDB\Driver\Manager irá mostrar vários detalhes sobre o gerenciador do driver que não são normalmente expostas. Isso pode ser útil para depurar como o driver visualiza a configuração do MongoDB e quais opções são usadas.

<?php

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

?>

O exemplo acima produzirá algo semelhante a:

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

Índice

adicionar nota

Notas de Usuários 1 note

up
8
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.
To Top