Skip to content

Commit 0f6786e

Browse files
(DOCS-13431): Multi-document transactions can implicitly create collections
1 parent 3e6bb3c commit 0f6786e

16 files changed

+208
-52
lines changed

source/core/transactions-in-applications.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ The new callback API incorporates logic:
117117
final MongoClient client = MongoClients.create(uri);
118118

119119
/*
120-
Prereq: Create collections. CRUD operations in transactions must be on existing collections.
120+
Create collections:
121121
*/
122122

123123
client.getDatabase("mydb1").getCollection("foo")
@@ -453,7 +453,7 @@ transactions:
453453
.. code-block:: javascript
454454
:copyable: false
455455

456-
// Prereq: Create collections. CRUD operations in transactions must be on existing collections.
456+
// Create collections:
457457
db.getSiblingDB("mydb1").foo.insert( {abc: 0}, { writeConcern: { w: "majority", wtimeout: 2000 } } );
458458
db.getSiblingDB("mydb2").bar.insert( {xyz: 0}, { writeConcern: { w: "majority", wtimeout: 2000 } } );
459459

@@ -480,4 +480,3 @@ transactions:
480480
session.commitTransaction();
481481

482482
session.endSession();
483-

source/core/transactions.txt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ of the transactions API:
9898
final MongoClient client = MongoClients.create(uri);
9999

100100
/*
101-
Prereq: Create collections. CRUD operations in transactions must be on existing collections.
101+
Create collections:
102102
*/
103103

104104
client.getDatabase("mydb1").getCollection("foo")
@@ -342,10 +342,7 @@ For transactions:
342342

343343
.. include:: /includes/extracts/transactions-operations-crud.rst
344344

345-
Operations that affect the database catalog, such as creating or
346-
dropping a collection or an index, are not allowed in
347-
transactions. For example, a transaction cannot include an insert
348-
operation that would result in the creation of a new collection. See
345+
For a list of operations not supported in transactions, see
349346
:ref:`transactions-ops-restricted`.
350347

351348
.. include:: /includes/extracts/transactions-operations-catalog-tip.rst
@@ -354,6 +351,23 @@ operation that would result in the creation of a new collection. See
354351

355352
- :doc:`Transactions and Operations Reference </core/transactions-operations>`
356353

354+
.. _transactions-create-collections-indexes:
355+
356+
Create Collections and Indexes In a Transaction
357+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
358+
359+
.. versionadded:: 4.4
360+
361+
When using :ref:`feature compatibility version (fcv) <view-fcv>`
362+
``"4.4"`` or greater, you can create a collection and index
363+
within a transaction. Specifically, you can:
364+
365+
.. include:: /includes/extracts/transactions-4.4-allowed-operations.rst
366+
367+
When using fcv ``"4.2"`` or less, no operations that affect the
368+
database catalog, such as creating or dropping a collection or an index,
369+
are allowed in transactions.
370+
357371
.. _transactions-ops-count:
358372

359373
Count Operation

source/includes/extracts-4.4-changes.yaml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,63 @@ content: |
311311
By default MongoDB does not set a minimum oplog retention period and
312312
automatically truncates the oplog starting with the oldest entries to
313313
maintain the configured maximum oplog size.
314+
---
315+
ref: 4.4-changes-transactions-upsert
316+
content: |
317+
318+
For :ref:`feature compatibility version (fcv) <view-fcv>` ``"4.4"``
319+
and greater, if the operation is specified on a non-existing
320+
collection and results in an upsert, the collection is implicitly
321+
created.
322+
323+
.. note::
324+
325+
.. include:: /includes/extracts/transactions-cross-shard-collection-restriction.rst
326+
327+
For fcv ``"4.2"`` or less, if the operation results in an upsert,
328+
the collection must already exist.
329+
---
330+
ref: 4.4-changes-transactions-insert
331+
content: |
332+
333+
For :ref:`feature compatibility version (fcv) <view-fcv>` ``"4.4"``
334+
and greater, if you specify an insert on a non-existing collection in
335+
a transaction, the collection is implicitly created.
336+
337+
.. note::
338+
339+
.. include:: /includes/extracts/transactions-cross-shard-collection-restriction.rst
340+
341+
For fcv ``"4.2"`` or less, insert operations that would result in
342+
the creation of a new collection are not allowed in a transaction.
343+
---
344+
ref: 4.4-changes-transactions-bulkWrite
345+
content: |
346+
347+
For :ref:`feature compatibility version (fcv) <view-fcv>` ``"4.4"``
348+
and greater, if an insert operation or update operation with
349+
``upsert: true`` is run in a transaction against a non-existing
350+
collection, the collection is implicitly created.
351+
352+
.. note::
353+
354+
.. include:: /includes/extracts/transactions-cross-shard-collection-restriction.rst
355+
356+
For fcv ``"4.2"`` or less, the collection must already exist for
357+
insert and ``upsert: true`` operations.
358+
---
359+
ref: 4.4-changes-transactions-save
360+
content: |
361+
362+
For :ref:`feature compatibility version (fcv) <view-fcv>` ``"4.4"``
363+
and greater, if you save a document to a non-existing collection in a
364+
transaction, the collection is implicitly created.
365+
366+
.. note::
367+
368+
.. include:: /includes/extracts/transactions-cross-shard-collection-restriction.rst
369+
370+
For fcv ``"4.2"`` or less, :method:`~db.collection.save()`
371+
operations that would result in the creation of a new collection are
372+
not allowed in a transaction.
314373
...

source/includes/extracts-transactions.yaml

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,56 @@ content: |
8080
---
8181
ref: transactions-operations-restrictions
8282
content: |
83+
84+
.. versionchanged:: 4.4
8385
8486
The following operations are not allowed in transactions:
8587
86-
- Operations that affect the database catalog, such as creating or
87-
dropping a collection or an index. For example, a
88-
transaction cannot include an insert operation that would result
89-
in the creation of a new collection.
90-
91-
The :dbcommand:`listCollections` and :dbcommand:`listIndexes`
92-
commands and their helper methods are also excluded.
88+
- Operations that affect the database catalog,
89+
such as creating or dropping a collection or an index, unless
90+
using :ref:`feature compatibility version (fcv) <view-fcv>`
91+
``"4.4"`` or greater. With fcv ``"4.4"`` or greater you can create
92+
collections and indexes in transactions. For details, see
93+
:ref:`transactions-create-collections-indexes`.
9394
94-
- Non-CRUD and non-informational operations, such as
95+
- Creating new collections in cross-shard write transactions. For
96+
example, if you write to an existing collection in one shard and
97+
implicitly create a collection in a different shard, MongoDB cannot
98+
perform both operations in the same transaction.
99+
100+
- The :dbcommand:`listCollections` and :dbcommand:`listIndexes`
101+
commands and their helper methods.
102+
103+
- Other non-CRUD and non-informational operations, such as
95104
:dbcommand:`createUser`, :dbcommand:`getParameter`,
96105
:dbcommand:`count`, etc. and their helpers.
97106
107+
---
108+
ref: transactions-4.4-allowed-operations
109+
content: |
110+
111+
- Create a collection in a transaction.
112+
113+
- Explicitly create a collection in a transaction using
114+
:method:`db.createCollection()`.
115+
116+
- Implicitly create a collection in a transaction by running an
117+
insert operation or update operation with ``upsert: true``
118+
against a non-existing collection.
119+
120+
.. note::
121+
122+
.. include:: /includes/extracts/transactions-cross-shard-collection-restriction.rst
123+
124+
- Create an index using :dbcommand:`createIndexes` on:
125+
126+
- A non-existing collection. In this case, the collection is
127+
created implicitly.
128+
129+
- An empty collection which was created earlier in the same
130+
multi-document transaction.
131+
132+
98133
---
99134
ref: transactions-operations-restrictions-info
100135
content: |
@@ -109,8 +144,20 @@ ref: transactions-operations-crud
109144
content: |
110145
111146
- You can specify read/write (CRUD) operations on **existing**
112-
collections. The collections can be in different databases. For a
113-
list of CRUD operations, see :ref:`transactions-operations-crud`.
147+
collections. For a list of CRUD operations, see
148+
:ref:`transactions-operations-crud`.
149+
150+
- When using :ref:`feature compatibility version (fcv) <view-fcv>`
151+
``"4.4"`` or greater, you can create collections and indexes
152+
in transactions. For details, see
153+
:ref:`transactions-create-collections-indexes`
154+
155+
- The collections used in a transaction can be in different
156+
databases.
157+
158+
.. note::
159+
160+
.. include:: /includes/extracts/transactions-cross-shard-collection-restriction.rst
114161
115162
- You cannot write to :doc:`capped </core/capped-collections>`
116163
collections. (Starting in MongoDB 4.2)
@@ -127,6 +174,14 @@ content: |
127174
- .. include:: /includes/extracts/transactions-killop-change.rst
128175
129176
---
177+
ref: transactions-cross-shard-collection-restriction
178+
content: |
179+
180+
You cannot create new collections in cross-shard write transactions.
181+
For example, if you write to an existing collection in one shard and
182+
implicitly create a collection in a different shard, MongoDB cannot
183+
perform both operations in the same transaction.
184+
---
130185
ref: transactions-operations-catalog-tip
131186
content: |
132187
@@ -502,4 +557,3 @@ content: |
502557
sharded cluster transaction includes some parts that use ``{w:
503558
"majority", j: true}`` write concern.
504559
...
505-

source/includes/table-transactions-operations.rst

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,36 @@
6969
7070
- :dbcommand:`insert`
7171

72-
- Only when run against an existing collection.
72+
- For :ref:`feature compatibility version (fcv) <view-fcv>`
73+
``"4.4"`` or greater, when run against a non-existing
74+
collection, the collection is implicitly created.
75+
76+
For fcv ``"4.2"`` or less, can only be run against an existing
77+
collection.
7378

7479
* - :method:`db.collection.save()`
7580
-
76-
- If an insert, only when run against an existing collection.
81+
- For :ref:`feature compatibility version (fcv) <view-fcv>`
82+
``"4.4"`` or greater, if an insert against a non-existing
83+
collection, the collection is implicitly created.
84+
85+
With fcv ``"4.2"`` or less, if an insert, can only be run against an
86+
existing collection.
7787

7888
* - | :method:`db.collection.updateOne()`
7989
| :method:`db.collection.updateMany()`
8090
| :method:`db.collection.replaceOne()`
8191
| :method:`db.collection.update()`
8292
8393
- :dbcommand:`update`
84-
- For ``upsert``, only when run against an existing collection.
94+
- For :ref:`feature compatibility version (fcv) <view-fcv>`
95+
``"4.4"`` or greater, if run with ``upsert: true`` against a
96+
non-existing collection, the collection is implicitly created.
97+
98+
For fcv ``"4.2"`` or less, if ``upsert: true``, the operation must be
99+
run against an existing collection.
85100

86101
* - | :method:`db.collection.bulkWrite()`
87102
| Various :doc:`/reference/method/js-bulk`
88103
-
89-
- | For insert operations, only when run against an existing collection.
90-
| For ``upsert``, only when run against an existing collection.
104+
- .. include:: /includes/extracts/4.4-changes-transactions-bulkWrite.rst

source/reference/command/insert.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ Transactions
125125

126126
.. include:: /includes/extracts/transactions-supported-operation.rst
127127

128-
The collection must already exist. An insert operation that
129-
would result in the creation of a new collection are not allowed in a
130-
transaction.
128+
.. include:: /includes/extracts/4.4-changes-transactions-insert.rst
131129

132130
.. include:: /includes/extracts/transactions-operations-write-concern.rst
133131

source/reference/command/update.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ Transactions
564564

565565
.. include:: /includes/extracts/transactions-supported-operation.rst
566566

567-
If the operation results in an upsert, the collection must already exist.
567+
.. include:: /includes/extracts/4.4-changes-transactions-upsert.rst
568568

569569
.. include:: /includes/extracts/transactions-operations-write-concern.rst
570570

source/reference/method/db.collection.bulkWrite.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,7 @@ Transactions
488488

489489
.. include:: /includes/extracts/transactions-supported-operation.rst
490490

491-
If run inside a transaction, the collection must already exist for
492-
insert and ``upsert: true`` operations.
491+
.. include:: /includes/extracts/4.4-changes-transactions-bulkWrite.rst
493492

494493
.. include:: /includes/extracts/transactions-operations-write-concern.rst
495494

source/reference/method/db.collection.insert.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ Transactions
125125

126126
.. include:: /includes/extracts/transactions-supported-operation.rst
127127

128-
The collection must already exist. An insert operation that
129-
would result in the creation of a new collection are not allowed in a
130-
transaction.
128+
.. include:: /includes/extracts/4.4-changes-transactions-insert.rst
131129

132130
.. include:: /includes/extracts/transactions-operations-write-concern.rst
133131

source/reference/method/db.collection.insertMany.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ Transactions
158158

159159
.. include:: /includes/extracts/transactions-supported-operation.rst
160160

161-
The collection must already exist. An insert operation that
162-
would result in the creation of a new collection are not allowed in a
163-
transaction.
161+
.. include:: /includes/extracts/4.4-changes-transactions-insert.rst
164162

165163
.. include:: /includes/extracts/transactions-operations-write-concern.rst
166164

@@ -404,5 +402,3 @@ This operation returns:
404402
},
405403
"errmsg" : "waiting for replication timed out"
406404
})
407-
408-

source/reference/method/db.collection.insertOne.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ Transactions
119119

120120
.. include:: /includes/extracts/transactions-supported-operation.rst
121121

122-
The collection must already exist. An insert operation that
123-
would result in the creation of a new collection are not allowed in a
124-
transaction.
122+
.. include:: /includes/extracts/4.4-changes-transactions-insert.rst
125123

126124
.. include:: /includes/extracts/transactions-operations-write-concern.rst
127125

source/reference/method/db.collection.save.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ Transactions
112112

113113
.. include:: /includes/extracts/transactions-supported-operation.rst
114114

115-
If the operation results in an insert, the collection must already
116-
exist.
115+
.. include:: /includes/extracts/4.4-changes-transactions-save.rst
117116

118117
.. include:: /includes/extracts/transactions-operations-write-concern.rst
119118

source/reference/method/db.collection.update.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,7 @@ Transactions
417417
Existing Collections and Transactions
418418
`````````````````````````````````````
419419

420-
Inside a transaction, you can specify read/write operations on existing
421-
collections. If the |operation| results in an
422-
upsert, the collection must already exist.
423-
424-
If the operation results in an upsert, the collection must already exist.
420+
.. include:: /includes/extracts/4.4-changes-transactions-upsert.rst
425421

426422
Write Concerns and Transactions
427423
````````````````````````````````

source/reference/method/db.collection.updateMany.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ Transactions
309309

310310
.. include:: /includes/extracts/transactions-supported-operation.rst
311311

312-
If the operation results in an upsert, the collection must already exist.
312+
.. include:: /includes/extracts/4.4-changes-transactions-upsert.rst
313313

314314
.. include:: /includes/extracts/transactions-operations-write-concern.rst
315315

source/reference/method/db.collection.updateOne.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,7 @@ Transactions
420420
Existing Collections and Transactions
421421
`````````````````````````````````````
422422

423-
Inside a transaction, you can specify read/write operations on existing
424-
collections. If the |operation| results in an
425-
upsert, the collection must already exist.
423+
.. include:: /includes/extracts/4.4-changes-transactions-upsert.rst
426424

427425
Write Concerns and Transactions
428426
````````````````````````````````

0 commit comments

Comments
 (0)