Navigation
This version of the documentation is archived and no longer supported. To learn how to upgrade your version of PHP Library Manual, refer to the upgrade documentation.

MongoDB\Client::startSession()

New in version 1.3.

Definition

MongoDB\Client::startSession

Start a new client session for use with this client.

function startSession(array $options = []): MongoDB\Driver\Session

Refer to the MongoDB\Driver\Manager::startSession() extension reference for accepted options.

Return Values

A MongoDBDriverSession object.

Errors/Exceptions

MongoDB\Driver\Exception\InvalidArgumentException for errors related to the parsing of parameters or options at the driver level.

MongoDB\Driver\Exception\RuntimeException for other errors at the driver level (e.g. connection errors).

Example

The following example starts a new session:

<?php

$client = new MongoDB\Client;

$session = $client->startSession();

var_dump($session);

The output would then resemble:

object(MongoDB\Driver\Session)#2043 (4) {
  ["logicalSessionId"]=>
  array(1) {
    ["id"]=>
    object(MongoDB\BSON\Binary)#225 (2) {
      ["data"]=>
      string(16) "................"
      ["type"]=>
      int(4)
    }
  }
  ["clusterTime"]=>
  NULL
  ["causalConsistency"]=>
  bool(true)
  ["operationTime"]=>
  NULL
}