PHP 8.3.0 RC 6 available for testing

The MongoDB\Driver\Manager class

(mongodb >=1.0.0)

简介

The MongoDB\Driver\Manager is the main entry point to the extension. It is responsible for maintaining connections to MongoDB (be it standalone server, replica set, or sharded cluster).

No connection to MongoDB is made upon instantiating the Manager. This means the MongoDB\Driver\Manager can always be constructed, even though one or more MongoDB servers are down.

Any write or query can throw connection exceptions as connections are created lazily. A MongoDB server may also become unavailable during the life time of the script. It is therefore important that all actions on the Manager to be wrapped in try/catch statements.

类摘要

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

示例

示例 #1 MongoDB\Driver\Manager::__construct() basic example

var_dump()ing a MongoDB\Driver\Manager will print out various details about the manager that are otherwise not normally exposed. This can be useful to debug how the driver views your MongoDB setup, and which options are used.

<?php

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

?>

以上示例的输出类似于:

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

目录

add a note

User Contributed Notes 3 notes

up
4
mike at eastghost dot com
5 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.
up
-8
gbormann dot nospam at telenet dot be
4 years ago
Re mocking implications of final nature of MongoDB\Driver\Manager :
Final classes can be mocked through an indirection using a wrapper class.
up
-21
Zalmoksis
5 years ago
It's worth noting that Manager is final, so you cannot mock it easily with tools like PhpUnit.
To Top