MongoDBClient-startSession.txt 1.64 KB
Newer Older
1 2 3 4 5 6
===============================
MongoDB\\Client::startSession()
===============================

.. versionadded:: 1.3

7 8
.. default-domain:: mongodb

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

.. phpmethod:: MongoDB\\Client::startSession()

   Start a new client session for use with this client.

   .. code-block:: php

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

   The ``$options`` parameter supports the following options:

   .. include:: /includes/apiargs/MongoDBClient-method-startSession-option.rst

Return Values
-------------

A :php:`MongoDB\Driver\Session <mongodb-driver-session>`

Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-driver-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

Example
-------

The following example starts a new session:

.. code-block:: php

   <?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
   }

See Also
--------

- :php:`MongoDB\\Driver\\Manager::startSession()
  <manual/en/mongodb-driver-manager.startsession.php>`
- :ref:`Causal Consistency <causal-consistency>` in the MongoDB manual