Documentation

Client
in package
implements Stringable

Tags
psalm-import-type

stage from Builder\Pipeline

psalm-no-seal-properties

Table of Contents

Interfaces

Stringable

Constants

DEFAULT_URI  = 'mongodb://127.0.0.1/'

Properties

$driverOptions  : DriverOptions
$manager  : Manager
$readConcern  : ReadConcern
$readPreference  : ReadPreference
$uri  : string
$writeConcern  : WriteConcern

Methods

__construct()  : mixed
Constructs a new Client instance.
__debugInfo()  : array<string|int, mixed>
Return internal properties for debugging purposes.
__get()  : Database
Select a database.
__toString()  : string
Return the connection string (i.e. URI).
addSubscriber()  : void
Registers a monitoring event subscriber with this Client's Manager
bulkWrite()  : BulkWriteCommandResult
Executes multiple write operations across multiple namespaces.
createClientEncryption()  : ClientEncryption
Returns a ClientEncryption instance for explicit encryption and decryption
dropDatabase()  : void
Drop a database.
getCollection()  : Collection
Returns a collection instance.
getDatabase()  : Database
Returns a database instance.
getManager()  : Manager
Return the Manager.
getReadConcern()  : ReadConcern
Return the read concern for this client.
getReadPreference()  : ReadPreference
Return the read preference for this client.
getTypeMap()  : array<string|int, mixed>
Return the type map for this client.
getWriteConcern()  : WriteConcern
Return the write concern for this client.
listDatabaseNames()  : Iterator<int, string>
List database names.
listDatabases()  : Iterator<int, DatabaseInfo>
List databases.
removeSubscriber()  : void
Unregisters a monitoring event subscriber with this Client's Manager
selectCollection()  : Collection
Select a collection.
selectDatabase()  : Database
Select a database.
startSession()  : Session
Start a new client session.
watch()  : ChangeStream
Create a change stream for watching changes to the cluster.

Constants

DEFAULT_URI

public mixed DEFAULT_URI = 'mongodb://127.0.0.1/'

Properties

$driverOptions

private DriverOptions $driverOptions

$manager

private Manager $manager

$readConcern

private ReadConcern $readConcern

$readPreference

private ReadPreference $readPreference

$writeConcern

private WriteConcern $writeConcern

Methods

__construct()

Constructs a new Client instance.

public __construct([string|null $uri = null ][, array<string|int, mixed> $uriOptions = [] ][, array<string|int, mixed> $driverOptions = [] ]) : mixed

This is the preferred class for connecting to a MongoDB server or cluster of servers. It serves as a gateway for accessing individual databases and collections.

Supported driver-specific options:

  • builderEncoder (MongoDB\Codec\Encoder): Encoder for query and aggregation builders. If not given, the default encoder will be used.

  • typeMap (array): Default type map for cursors and BSON documents.

Other options are documented in MongoDB\Driver\Manager::__construct().

Parameters
$uri : string|null = null

MongoDB connection string. If none is provided, this defaults to self::DEFAULT_URI.

$uriOptions : array<string|int, mixed> = []

Additional connection string options

$driverOptions : array<string|int, mixed> = []

Driver-specific options

Tags
see
https://mongodb.com/docs/manual/reference/connection-string/
see
https://php.net/manual/en/mongodb-driver-manager.construct.php
see
https://php.net/manual/en/mongodb.persistence.php#mongodb.persistence.typemaps
throws
InvalidArgumentException

for parameter/option parsing errors

throws
InvalidArgumentException

for parameter/option parsing errors in the driver

throws
RuntimeException

for other driver errors (e.g. connection errors)

__toString()

Return the connection string (i.e. URI).

public __toString() : string
Return values
string

addSubscriber()

Registers a monitoring event subscriber with this Client's Manager

public final addSubscriber(Subscriber $subscriber) : void
Parameters
$subscriber : Subscriber
Tags
see
Manager::addSubscriber()

bulkWrite()

Executes multiple write operations across multiple namespaces.

public bulkWrite(BulkWriteCommand|ClientBulkWrite $bulk[, array<string|int, mixed> $options = [] ]) : BulkWriteCommandResult
Parameters
$bulk : BulkWriteCommand|ClientBulkWrite

Assembled bulk write command or builder

$options : array<string|int, mixed> = []

Additional options

Tags
throws
UnsupportedException

if options are unsupported on the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

see
ClientBulkWriteCommand::__construct()

for supported options

Return values
BulkWriteCommandResult

createClientEncryption()

Returns a ClientEncryption instance for explicit encryption and decryption

public createClientEncryption(Manager} $options) : ClientEncryption
Parameters
$options : Manager}
Return values
ClientEncryption

dropDatabase()

Drop a database.

public dropDatabase(string $databaseName[, array<string|int, mixed> $options = [] ]) : void
Parameters
$databaseName : string

Database name

$options : array<string|int, mixed> = []

Additional options

Tags
see
DropDatabase::__construct()

for supported options

throws
UnsupportedException

if options are unsupported on the selected server

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

getCollection()

Returns a collection instance.

public getCollection(string $databaseName, string $collectionName[, array<string|int, mixed> $options = [] ]) : Collection

If the collection does not exist in the database, it is not created when invoking this method.

Parameters
$databaseName : string
$collectionName : string
$options : array<string|int, mixed> = []
Tags
see
Collection::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

Return values
Collection

getDatabase()

Returns a database instance.

public getDatabase(string $databaseName[, array<string|int, mixed> $options = [] ]) : Database

If the database does not exist on the server, it is not created when invoking this method.

Parameters
$databaseName : string
$options : array<string|int, mixed> = []
Tags
see
Database::__construct()

for supported options

Return values
Database

getManager()

Return the Manager.

public getManager() : Manager
Return values
Manager

getReadPreference()

Return the read preference for this client.

public getReadPreference() : ReadPreference
Return values
ReadPreference

getTypeMap()

Return the type map for this client.

public getTypeMap() : array<string|int, mixed>
Return values
array<string|int, mixed>

listDatabaseNames()

List database names.

public listDatabaseNames([array<string|int, mixed> $options = [] ]) : Iterator<int, string>
Parameters
$options : array<string|int, mixed> = []
Tags
see
ListDatabaseNames::__construct()

for supported options

throws
UnexpectedValueException

if the command response was malformed

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
Iterator<int, string>

listDatabases()

List databases.

public listDatabases([array<string|int, mixed> $options = [] ]) : Iterator<int, DatabaseInfo>
Parameters
$options : array<string|int, mixed> = []
Tags
see
ListDatabases::__construct()

for supported options

throws
UnexpectedValueException

if the command response was malformed

throws
InvalidArgumentException

for parameter/option parsing errors

throws
RuntimeException

for other driver errors (e.g. connection errors)

Return values
Iterator<int, DatabaseInfo>

removeSubscriber()

Unregisters a monitoring event subscriber with this Client's Manager

public final removeSubscriber(Subscriber $subscriber) : void
Parameters
$subscriber : Subscriber
Tags
see
Manager::removeSubscriber()

selectCollection()

Select a collection.

public selectCollection(string $databaseName, string $collectionName[, array<string|int, mixed> $options = [] ]) : Collection
Parameters
$databaseName : string

Name of the database containing the collection

$collectionName : string

Name of the collection to select

$options : array<string|int, mixed> = []

Collection constructor options

Tags
see
Collection::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

Return values
Collection

selectDatabase()

Select a database.

public selectDatabase(string $databaseName[, array<string|int, mixed> $options = [] ]) : Database
Parameters
$databaseName : string

Name of the database to select

$options : array<string|int, mixed> = []

Database constructor options

Tags
see
Database::__construct()

for supported options

throws
InvalidArgumentException

for parameter/option parsing errors

Return values
Database

watch()

Create a change stream for watching changes to the cluster.

public watch([array<string|int, mixed> $pipeline = [] ][, array<string|int, mixed> $options = [] ]) : ChangeStream
Parameters
$pipeline : array<string|int, mixed> = []
$options : array<string|int, mixed> = []

Command options

Tags
see
Watch::__construct()

for supported options

psalm-param

list $pipeline Aggregation pipeline

throws
InvalidArgumentException

for parameter/option parsing errors

Return values
ChangeStream

        
On this page

Search results