|
44 | 44 | Transactions
|
45 | 45 | ============
|
46 | 46 |
|
| 47 | +.. versionadded:: 3.7 |
| 48 | +
|
47 | 49 | MongoDB 4.0 adds support for transactions on replica set primaries. A
|
48 | 50 | transaction is associated with a :class:`ClientSession`. To start a transaction
|
49 | 51 | on a session, use :meth:`ClientSession.start_transaction` in a with-statement.
|
|
76 | 78 | A session may only have a single active transaction at a time, multiple
|
77 | 79 | transactions on the same session can be executed in sequence.
|
78 | 80 |
|
79 |
| -.. versionadded:: 3.7 |
80 |
| -
|
81 | 81 | Sharded Transactions
|
82 | 82 | ^^^^^^^^^^^^^^^^^^^^
|
83 | 83 |
|
| 84 | +.. versionadded:: 3.9 |
| 85 | +
|
84 | 86 | PyMongo 3.9 adds support for transactions on sharded clusters running MongoDB
|
85 |
| -4.2. Sharded transactions have the same API as replica set transactions. |
| 87 | +>=4.2. Sharded transactions have the same API as replica set transactions. |
86 | 88 | When running a transaction against a sharded cluster, the session is
|
87 | 89 | pinned to the mongos server selected for the first operation in the
|
88 | 90 | transaction. All subsequent operations that are part of the same transaction
|
89 | 91 | are routed to the same mongos server. When the transaction is completed, by
|
90 | 92 | running either commitTransaction or abortTransaction, the session is unpinned.
|
91 | 93 |
|
92 |
| -.. versionadded:: 3.9 |
93 |
| -
|
94 | 94 | .. mongodoc:: transactions
|
95 | 95 |
|
| 96 | +Snapshot Reads |
| 97 | +============== |
| 98 | +
|
| 99 | +.. versionadded:: 3.12 |
| 100 | +
|
| 101 | +MongoDB 5.0 adds support for snapshot reads. Snapshot reads are requested by |
| 102 | +passing the ``snapshot`` option to |
| 103 | +:meth:`~pymongo.mongo_client.MongoClient.start_session`. |
| 104 | +If ``snapshot`` is True, all read operations that use this session read data |
| 105 | +from the same snapshot timestamp. The server chooses the latest |
| 106 | +majority-committed snapshot timestamp when executing the first read operation |
| 107 | +using the session. Subsequent reads on this session read from the same |
| 108 | +snapshot timestamp. Snapshot reads are also supported when reading from |
| 109 | +replica set secondaries. |
| 110 | +
|
| 111 | +.. code-block:: python |
| 112 | +
|
| 113 | + # Each read using this session reads data from the same point in time. |
| 114 | + with client.start_session(snapshot=True) as session: |
| 115 | + order = orders.find_one({"sku": "abc123"}, session=session) |
| 116 | + inventory = inventory.find_one({"sku": "abc123"}, session=session) |
| 117 | +
|
| 118 | +Snapshot Reads Limitations |
| 119 | +^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 120 | +
|
| 121 | +Snapshot reads sessions are incompatible with ``causal_consistency=True``. |
| 122 | +Only the following read operations are supported in a snapshot reads session: |
| 123 | +
|
| 124 | +- :meth:`~pymongo.collection.Collection.find` |
| 125 | +- :meth:`~pymongo.collection.Collection.find_one` |
| 126 | +- :meth:`~pymongo.collection.Collection.aggregate` |
| 127 | +- :meth:`~pymongo.collection.Collection.count_documents` |
| 128 | +- :meth:`~pymongo.collection.Collection.distinct` (on unsharded collections) |
| 129 | +
|
96 | 130 | Classes
|
97 | 131 | =======
|
98 | 132 | """
|
|
0 commit comments