- Transactions >
- Transactions and Operations
Transactions and Operations¶
On this page
For transactions:
- You can specify read/write (CRUD) operations on existing collections. The collections can be in different databases. For a list of CRUD operations, see CRUD Operations.
- You cannot write to capped collections. (Starting in MongoDB 4.2)
- You cannot read/write to collections in the
config
,admin
, orlocal
databases. - You cannot write to
system.*
collections. - You cannot return the supported operation’s query plan (i.e.
explain
).
- For cursors created outside of a transaction, you cannot call
getMore
inside the transaction. - For cursors created in a transaction, you cannot call
getMore
outside the transaction. - Starting in MongoDB 4.2, you cannot specify
killCursors
as the first operation in a transaction.
Operations that affect the database catalog, such as creating or dropping a collection or an index, are not allowed in multi-document transactions. For example, a multi-document transaction cannot include an insert operation that would result in the creation of a new collection. See Restricted Operations.
Operations Supported in Multi-Document Transactions¶
CRUD Operations¶
The following read/write operations are allowed in transactions:
Method | Command | Note |
---|---|---|
db.collection.aggregate() |
aggregate |
Excluding the following stages: |
db.collection.countDocuments() |
Excluding the following query operator expressions: The method uses the |
|
db.collection.distinct() |
distinct |
Available on unsharded collections. For sharded collections, use the aggregation pipeline with the
$group stage. See Distinct Operation. |
db.collection.find() |
find |
|
geoSearch |
||
delete |
||
findAndModify |
For upsert , only when run against an existing collection. |
|
insert |
Only when run against an existing collection. | |
db.collection.save() |
If an insert, only when run against an existing collection. | |
update |
For upsert , only when run against an existing collection. |
|
For insert operations, only when run against an existing collection.
For
upsert , only when run against an existing collection. |
Updates to Shard Key Values
Starting in MongoDB 4.2, you can update a document’s shard key value
(unless the shard key field is the immutable _id
field) by
issuing single-document update/findAndModify
operations either in a transaction or as a retryable write. For details, see Change a Document’s Shard Key Value.
Count Operation¶
To perform a count operation within a transaction, use the
$count
aggregation stage or the $group
(with a
$sum
expression) aggregation stage.
MongoDB drivers compatible with the 4.0 features provide a
collection-level API countDocuments(filter, options)
as a helper
method that uses the $group
with a $sum
expression
to perform a count. The 4.0 drivers have deprecated the count()
API.
Starting in MongoDB 4.0.3, the mongo
shell provides the
db.collection.countDocuments()
helper method that uses the
$group
with a $sum
expression to perform a count.
Distinct Operation¶
To perform a distinct operation within a transaction:
For unsharded collections, you can use the
db.collection.distinct()
method/thedistinct
command as well as the aggregation pipeline with the$group
stage.For sharded collections, you cannot use the
db.collection.distinct()
method or thedistinct
command.To find the distinct values for a sharded collection, use the aggregation pipeline with the
$group
stage instead. For example:Instead of
db.coll.distinct("x")
, useInstead of
db.coll.distinct("x", { status: "A" })
, use:
The pipeline returns a cursor to a document:
Iterate the cursor to access the results document.
Informational Operations¶
Informational commands, such as hello
,
buildInfo
, connectionStatus
(and their
helper methods) are allowed in transactions; however, they cannot be
the first operation in the transaction.
Restricted Operations¶
The following operations are not allowed in transactions:
Operations that affect the database catalog, such as creating or dropping a collection or an index. For example, a transaction cannot include an insert operation that would result in the creation of a new collection.
The
listCollections
andlistIndexes
commands and their helper methods are also excluded.Non-CRUD and non-informational operations, such as
createUser
,getParameter
,count
, etc. and their helpers.