Transactions and Operations
For transactions:
- You can specify read/write (CRUD) operations on existingcollections. The collections can be in different databases. For alist of CRUD operations, see CRUD Operations.
- You cannot write to cappedcollections. (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
asthe first operation in a transaction.
Operations that affect the database catalog, such as creating ordropping a collection or an index, are not allowed in multi-documenttransactions. For example, a multi-document transaction cannot includean insert operation that would result in the creation of a newcollection. See Restricted Operations.
Operations Supported in Multi-Document Transactions
CRUD Operations
The following read/write operations are allowed in transactions:
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 acollection-level API countDocuments(filter, options)
as a helpermethod that uses the $group
with a $sum
expressionto perform a count. The 4.0 drivers have deprecated the count()
API.
Starting in MongoDB 4.0.3, the mongo
shell provides thedb.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 pipelinewith 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 theaggregation pipeline with the $group
stage instead.For example:
- Instead of
db.coll.distinct("x")
, use
- db.coll.aggregate([
- { $group: { _id: null, distinctValues: { $addToSet: "$x" } } },
- { $project: { _id: 0 } }
- ])
- Instead of
db.coll.distinct("x", { status: "A" })
, use:
- db.coll.aggregate([
- { $match: { status: "A" } },
- { $group: { _id: null, distinctValues: { $addToSet: "$x" } } },
- { $project: { _id: 0 } }
- ])
The pipeline returns a cursor to a document:
- { "distinctValues" : [ 2, 3, 1 ] }
Iterate the cursor to access the results document.
Informational Operations
Informational commands, such as isMaster
,buildInfo
, connectionStatus
(and theirhelper methods) are allowed in transactions; however, they cannot bethe first operation in the transaction.
Restricted Operations
The following operations are not allowed in transactions:
- Operations that affect the database catalog, such as creating ordropping a collection or an index. For example, atransaction cannot include an insert operation that would resultin the creation of a new collection.
The listCollections
and listIndexes
commands and their helper methods are also excluded.
- Non-CRUD and non-informational operations, such as
createUser
,getParameter
,count
, etc. and their helpers.
See also