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
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.