|
| 1 | +============ |
| 2 | +Transactions |
| 3 | +============ |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. meta:: |
| 14 | + :description: transactions, multi-document transactions, multi-statement transactions |
| 15 | + :keywords: transactions, multi-document transactions, multi-statement transactions |
| 16 | + |
| 17 | +.. versionadded:: 4.0 |
| 18 | + |
| 19 | +In MongoDB, an operation on a single document is atomic. Because you can |
| 20 | +use embedded documents and arrays to capture relationships between data |
| 21 | +in a single document structure instead of normalizing across multiple |
| 22 | +documents and collections, this single-document atomicity obviates the |
| 23 | +need for multi-document transactions for many practical use cases. |
| 24 | + |
| 25 | +However, for situations that require atomicity for updates to multiple |
| 26 | +documents or consistency between reads to multiple documents, MongoDB |
| 27 | +provides the ability to perform multi-document transactions against |
| 28 | +replica sets. Multi-document transactions can be used across multiple |
| 29 | +operations, collections, and documents. Multi-document transactions |
| 30 | +provide an "all-or-nothing" proposition. When a transaction commits, |
| 31 | +all data changes made in the transaction are saved. If any operation in |
| 32 | +the transaction fails, the transaction aborts and all data changes made |
| 33 | +in the transaction are discarded without ever becoming visible. Until a |
| 34 | +transaction commits, no write operations in the transaction are visible |
| 35 | +outside the transaction. |
| 36 | + |
| 37 | +.. important:: |
| 38 | + |
| 39 | + Multi-document transaction incurs a greater performance cost over |
| 40 | + single document writes, and the availability of multi-document |
| 41 | + transaction should not be a replacement for effective schema design. |
| 42 | + For many scenarios, the denormalized data model will continue to be |
| 43 | + optimal for your data and use cases. |
| 44 | + |
| 45 | +Transaction and Replica Sets |
| 46 | +---------------------------- |
| 47 | + |
| 48 | +Multi-document transactions are available for replica sets only. |
| 49 | + |
| 50 | +The ``featureCompatibilityVersion`` of all members of the replica set |
| 51 | +must be ``4.0`` or greater. To check the |
| 52 | +``featureCompatibilityVersion`` for a member, connect to the member and |
| 53 | +run the following command: |
| 54 | + |
| 55 | +.. code-block:: javascript |
| 56 | + |
| 57 | + db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) |
| 58 | + |
| 59 | +For more information on the ``featureCompatibilityVersion`` flag, see |
| 60 | +:dbcommand:`setFeatureCompatibilityVersion`. |
| 61 | + |
| 62 | +.. _txn-operations: |
| 63 | + |
| 64 | +Transaction and CRUD Operations |
| 65 | +------------------------------- |
| 66 | + |
| 67 | +For multi-document transactions, you can only specify read/write (CRUD) |
| 68 | +operations on existing collections. |
| 69 | + |
| 70 | +Operations that affect the database catalog, such as creating or |
| 71 | +dropping a collection or an index, are not allowed in multi-document |
| 72 | +transactions. For example, a multi-document transaction cannot include |
| 73 | +an insert operation that would result in the creation of a new |
| 74 | +collection. |
| 75 | + |
| 76 | +.. include:: /includes/transaction-operations.rst |
| 77 | + |
| 78 | +Transactions and Sessions |
| 79 | +------------------------- |
| 80 | + |
| 81 | +Transactions are associated with a session. That is, you start a |
| 82 | +transaction for a session. At any given time, you can have at most one |
| 83 | +open transaction for a session. |
| 84 | + |
| 85 | + |
| 86 | +Atomicity |
| 87 | +--------- |
| 88 | + |
| 89 | +Multi-document transactions are atomic: |
| 90 | + |
| 91 | +- When a transaction commits, all data changes made in the transaction |
| 92 | + are saved and visible outside the transaction. Until a transaction |
| 93 | + commits, the data changes made in the transaction are not visible |
| 94 | + outside the transaction. |
| 95 | + |
| 96 | +- When a transaction aborts, all data changes made in the transaction |
| 97 | + are discarded without ever becoming visible. For example, if any |
| 98 | + operation in the transaction fails, the transaction aborts and all |
| 99 | + data changes made in the transaction are discarded without ever |
| 100 | + becoming visible. |
| 101 | + |
| 102 | +Transactions and MongoDB Drivers |
| 103 | +-------------------------------- |
| 104 | + |
| 105 | +Clients require MongoDB drivers updated for MongoDB 4.0. |
| 106 | + |
| 107 | +To associate read and write operations with an open transaction, you |
| 108 | +pass the session to the operations. |
| 109 | + |
| 110 | +Transactions and the ``mongo`` Shell |
| 111 | +------------------------------------ |
| 112 | + |
| 113 | +The following :binary:`~bin.mongo` shell methods are available for |
| 114 | +transactions: |
| 115 | + |
| 116 | +- :method:`Session.startTransaction()` |
| 117 | + |
| 118 | +- :method:`Session.commitTransaction()` |
| 119 | + |
| 120 | +- :method:`Session.abortTransaction()` |
| 121 | + |
| 122 | +.. _txn-read-concern: |
| 123 | + |
| 124 | +Read Concern |
| 125 | +------------ |
| 126 | + |
| 127 | +Multi-document transactions support read concern |
| 128 | +:readconcern:`"snapshot"`, :readconcern:`"local"`, and |
| 129 | +:readconcern:`"majority"`. For :readconcern:`"local"` and |
| 130 | +:readconcern:`"majority"` read concern, MongoDB may sometimes |
| 131 | +substitute a stronger read concern. |
| 132 | + |
| 133 | +.. note:: |
| 134 | + |
| 135 | + You set the read concern at the transaction level, not at the |
| 136 | + individual operation level. The operations in the transaction will |
| 137 | + use the the transaction level read concern; i.e. the transaction |
| 138 | + level read concern overrides specific operation level read concern. |
| 139 | + |
0 commit comments