Skip to content

Commit 1daa9e2

Browse files
committed
DOCS-11540, DOCS-11605: mongo shell txn helpers and read concern
1 parent 1f65f00 commit 1daa9e2

15 files changed

+774
-138
lines changed

config/redirects

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,10 @@ raw: /master/release-notes/3.0-general-improvements -> ${base}/release-notes/3.0
13911391
[v3.6-*]: /${version}/reference/method/cursor.snapshot -> ${base}/${version}/reference/method/
13921392
[v3.6-*]: /${version}/administration/replica-sets -> ${base}/${version}/replication
13931393

1394+
# Redirects for 4.0
1395+
1396+
[v4.0-v4.0]: /${version}/upcoming.txt -> ${base}/${version}/core/transactions
1397+
13941398
# Redirects for 4.0 and greater
13951399

13961400
[v4.0-*]: /${version}/core/security-mongodb-cr -> ${base}/${version}/core/security-scram
@@ -1399,8 +1403,14 @@ raw: /master/release-notes/3.0-general-improvements -> ${base}/release-notes/3.0
13991403
[v4.0-*]: /${version}/reference/command/resync -> ${base}/${version}/core/master-slave
14001404
[v4.0-*]: /${version}/reference/program/mongoperf -> ${base}/${version}/reference/program
14011405

1402-
# Redirects for 3.6 or earlier
1406+
# Redirects for 3.6 or earlier (i.e. before 4.0)
1407+
14031408
[*-v3.6]: /${version}/reference/read-concern-snapshot -> ${base}/${version}/reference/read-concern
1409+
[*-v3.6]: /${version}/core/transactions -> ${base}/${version}/core/write-operations-atomicity
1410+
[*-v3.6]: /${version}/reference/method/Session.abortTransaction -> ${base}/${version}/core/write-operations-atomicity
1411+
[*-v3.6]: /${version}/reference/method/Session.commitTransaction -> ${base}/${version}/core/write-operations-atomicity
1412+
[*-v3.6]: /${version}/reference/method/Session.startTransaction -> ${base}/${version}/core/write-operations-atomicity
1413+
14041414

14051415
# 2.8 compatibility
14061416
[*]: /${version}/release-notes/2.8-downgrade -> ${base}/${version}/release-notes

source/contents.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ project, this Manual and additional editions of this text.
77

88
.. toctree::
99
:titlesonly:
10-
11-
/upcoming
10+
1211
Introduction </introduction>
1312
Installation </installation>
1413
/mongo
1514
/crud
1615
/aggregation
1716
/data-modeling
17+
/core/transactions
1818
/indexes
1919
/security
2020
/changeStreams

source/core/transactions.txt

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.. list-table::
2+
:header-rows: 1
3+
:widths: 50 20 30
4+
5+
* - Method
6+
- Command
7+
- Note
8+
9+
* - :method:`db.collection.aggregate()`
10+
- :dbcommand:`aggregate`
11+
-
12+
13+
* - :method:`cursor.count()`
14+
- :dbcommand:`count`
15+
-
16+
17+
18+
* - :method:`db.collection.distinct()`
19+
- :dbcommand:`distinct`
20+
-
21+
22+
* - :method:`db.collection.find()`
23+
- :dbcommand:`find`
24+
-
25+
26+
* -
27+
- :dbcommand:`geoSearch`
28+
-
29+
30+
* - | :method:`db.collection.deleteMany()`
31+
| :method:`db.collection.deleteOne()`
32+
| :method:`db.collection.remove()`
33+
34+
- :dbcommand:`delete`
35+
-
36+
37+
* - | :method:`db.collection.findOneAndDelete()`
38+
| :method:`db.collection.findOneAndReplace()`
39+
| :method:`db.collection.findOneAndUpdate()`
40+
41+
- :dbcommand:`findAndModify`
42+
-
43+
44+
* - | :method:`db.collection.insertMany()`
45+
| :method:`db.collection.insertOne()`
46+
| :method:`db.collection.insert()`
47+
48+
- :dbcommand:`insert`
49+
50+
- Only when run against an existing collection.
51+
52+
53+
* - | :method:`db.collection.updateOne()`
54+
| :method:`db.collection.updateMany()`
55+
| :method:`db.collection.update()`
56+
57+
- :dbcommand:`update`
58+
- For ``upsert``, only when run against an existing collection.
59+
60+
* - | :method:`db.collection.bulkWrite()`
61+
| Various :doc:`/reference/method/js-bulk`
62+
-
63+
- For insert operations, only when run against an existing collection.

source/index.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ The MongoDB |version| Dev Series Manual
1111

1212
For new features in MongoDB 3.7.x, see :doc:`/release-notes/4.0`.
1313

14-
.. admonition:: Beta Program
15-
:class: note
16-
17-
The upcoming MongoDB 4.0 will add :doc:`multi-document transactions
18-
</upcoming>` for replica sets. Sign up for the `multi-document
19-
transactions beta program <https://www.mongodb.com/transactions?jmp=docs>`__.
20-
21-
.. include:: /includes/fact-upcoming.rst
22-
2314
Welcome to the MongoDB |version| Manual! MongoDB is an open-source,
2415
document database designed for ease of development and scaling. The
2516
Manual introduces key concepts in MongoDB, presents the query language,
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
==========================
2+
Session.abortTransaction()
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+
Definition
14+
----------
15+
16+
.. method:: Session.abortTransaction()
17+
18+
.. versionadded:: 4.0
19+
20+
:doc:`/core/transactions` are available for replica sets.
21+
22+
Terminates the :doc:`multi-document transaction
23+
</core/transactions>` and rolls back any data changes made by the
24+
operations within the transaction. That is, the transaction ends
25+
without saving any of the changes made by the operations in the
26+
transaction.
27+
28+
Behavior
29+
--------
30+
31+
Write Concern
32+
~~~~~~~~~~~~~
33+
34+
When aborting the transaction, the session uses the write concern
35+
specified at the transaction start. See
36+
:method:`Session.startTransaction()`.
37+
38+
Atomicity
39+
~~~~~~~~~
40+
41+
When a transaction aborts, all data changes made by the writes in the
42+
transaction are discarded without ever becoming visible and the
43+
transaction ends.
44+
45+
Example
46+
-------
47+
48+
Consider a scenario where as changes are made to an employee's record
49+
in the ``hr`` database, you want to ensure that the ``events``
50+
collection in the ``reporting`` database are in sync with the ``hr``
51+
changes and vice versa. That is, you want to ensure that these writes are done as a
52+
single transaction, such that either both operations succeed or fail.
53+
54+
The ``employees`` collection in the ``hr`` database has the following
55+
documents:
56+
57+
.. code-block:: javascript
58+
59+
{ "_id" : ObjectId("5af0776263426f87dd69319a"), "employee" : 3, "name" : { "title" : "Mr.", "name" : "Iba Ochs" }, "status" : "Active", "department" : "ABC" }
60+
{ "_id" : ObjectId("5af0776263426f87dd693198"), "employee" : 1, "name" : { "title" : "Miss", "name" : "Ann Thrope" }, "status" : "Active", "department" : "ABC" }
61+
{ "_id" : ObjectId("5af0776263426f87dd693199"), "employee" : 2, "name" : { "title" : "Mrs.", "name" : "Eppie Delta" }, "status" : "Active", "department" : "XYZ" }
62+
63+
The ``employees`` collection has a unique index on the ``employee`` field.
64+
65+
The ``events`` collection in the ``reporting`` database has the
66+
following documents:
67+
68+
.. code-block:: javascript
69+
70+
{ "_id" : ObjectId("5af07daa051d92f02462644a"), "employee" : 1, "status" : { "new" : "Active", "old" : null }, "department" : { "new" : "ABC", "old" : null } }
71+
{ "_id" : ObjectId("5af07daa051d92f02462644b"), "employee" : 2, "status" : { "new" : "Active", "old" : null }, "department" : { "new" : "XYZ", "old" : null } }
72+
{ "_id" : ObjectId("5af07daa051d92f02462644c"), "employee" : 3, "status" : { "new" : "Active", "old" : null }, "department" : { "new" : "ABC", "old" : null } }
73+
74+
The following example opens a transaction, attempts to add a record to
75+
the ``events`` collection and add a document to the ``employees``
76+
collection. If the operation encounters an error in either operations
77+
or in committing the transaction, the session aborts the transaction.
78+
79+
.. code-block:: javascript
80+
81+
// Start a session.
82+
session = db.getMongo().startSession( );
83+
84+
employeesCollection = session.getDatabase("hr").employees;
85+
eventsCollection = session.getDatabase("reporting").events;
86+
87+
// Start a transaction for the session that uses:
88+
// - read concern "snapshot"
89+
// - write concern "majority"
90+
session.startTransaction( {
91+
readConcern: { level: "snapshot" },
92+
writeConcern: { w: "majority" }
93+
} );
94+
95+
try{
96+
97+
eventsCollection.insertOne(
98+
{ employee: 3, status: { new: "Active", old: null }, department: { new: "XYZ", old: null } }
99+
);
100+
101+
// Count number of events for employee 3
102+
var count = eventsCollection.find( { employee: 3 } ).count();
103+
print ( "Within open transaction: " + count );
104+
105+
// The following operations should fail as an employee ``3`` already exist in employees collection
106+
employeesCollection.insertOne(
107+
{ employee: 3, name: { title: "Miss", name: "Terri Bachs" }, status: "Active", department: "XYZ" }
108+
);
109+
110+
} catch (error) {
111+
print ("Abort transaction.")
112+
session.abortTransaction(); // Uses write concern "majority"
113+
}
114+
115+
// Until commit, the data changes are not visible outside this transaction
116+
session.commitTransaction(); // Uses write concern "majority"
117+
118+
var employeeCount = eventsCollection.find( { employees: 3 } ).count();
119+
120+
print ("After abort transaction: " + employeeCount);
121+
122+
session.endSession();
123+
124+
.. seealso::
125+
126+
- :method:`Session.startTransaction()`
127+
128+
- :method:`Session.commitTransaction()`

0 commit comments

Comments
 (0)