php8.5
Home/ Manual/ driver / manager/ MongoDB\Driver\Manager::startSession

MongoDB\Driver\Manager::startSession

PHP function Edit on GitHub ✎

(mongodb >=1.4.0)

Start a new client session for use with this client

Description

MongoDB\Driver\Manager::startSession(array|null $options = null): MongoDB\Driver\Session

Creates a MongoDB\Driver\Session for the given options. The session may then be specified when executing commands, queries, and write operations.

Note

A MongoDB\Driver\Session can only be used with the MongoDB\Driver\Manager from which it was created.

Parameters

options
OptionTypeDescriptionDefault
OptionTypeDescription
causalConsistencyboolConfigure causal consistency in a session. If true, each operation in the session will be causally ordered after the previous read or write operation. Set to false to disable causal consistency.See Causal Consistency in the MongoDB manual for more information.true
defaultTransactionOptionsarrayDefault options to apply to newly created transactions. These options are used unless they are overridden when a transaction is started with different value for each option.optionsOptionTypeDescriptionMaxCommitTimeMS ReadConcern ReadPreference WriteConcernThis option is available in MongoDB 4.0+.[]
snapshotboolConfigure snapshot reads in a session. If true, a timestamp will be obtained from the first supported read operation in the session (i.e. find, aggregate, or unsharded distinct). Subsequent read operations within the session will then utilize a "snapshot" read concern level to read majority-committed data from that timestamp. Set to false to disable snapshot reads.Snapshot reads require MongoDB 5.0+ and cannot be used with causal consistency, transactions, or write operations. If "snapshot" is true, "causalConsistency" will default to false.See Read Concern "snapshot" in the MongoDB manual for more information.false

Return Values

Returns a MongoDB\Driver\Session.

Errors/Exceptions

  • Throws MongoDB\Driver\Exception\InvalidArgumentException if the "causalConsistency" and "snapshot" options are both true.
  • Throws MongoDB\Driver\Exception\RuntimeException if the session could not be created (e.g. libmongoc does not support crypto).

Changelog

VersionDescription
PECL mongodb 1.11.0The "snapshot" option was added.
PECL mongodb 1.6.0The "maxCommitTimeMS" option was added to "defaultTransactionOptions".
PECL mongodb 1.5.0The "defaultTransactionOptions" option was added.

See Also

  • MongoDB\Driver\Session
  • Causal Consistency in the MongoDB manual

Source: reference/mongodb/mongodb/driver/manager/startsession.xml · from the official PHP manual (php/doc-en)