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,or local 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 callgetMore inside the transaction.
  • For cursors created in a transaction, you cannot callgetMore 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:

MethodCommandNote
db.collection.aggregate()aggregateExcluding the following stages:- $collStats- $currentOp- $indexStats- $listLocalSessions- $listSessions- $merge- $out- $planCacheStats
db.collection.countDocuments() Excluding the following query operator expressions:- $where- $near- $nearSphereThe method uses the $match aggregation stage for thequery and $group aggregation stage with a$sum expression to perform the count.
db.collection.distinct()distinctAvailable on unsharded collections.For sharded collections, use the aggregation pipeline with the$group stage. See Distinct Operation.
db.collection.find()find
geoSearch
db.collection.deleteMany()db.collection.deleteOne()db.collection.remove()delete
db.collection.findOneAndDelete()db.collection.findOneAndReplace()db.collection.findOneAndUpdate()findAndModifyFor upsert, only when run against an existing collection.
db.collection.insertMany()db.collection.insertOne()db.collection.insert()insertOnly when run against an existing collection.
db.collection.save() If an insert, only when run against an existing collection.
db.collection.updateOne()db.collection.updateMany()db.collection.replaceOne()db.collection.update()updateFor upsert, only when run against an existing collection.
db.collection.bulkWrite()Various Bulk Operation Methods For insert operations, only when run against an existing collection.For upsert, only when run against an existing collection.

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:

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
  1. db.coll.aggregate([
  2. { $group: { _id: null, distinctValues: { $addToSet: "$x" } } },
  3. { $project: { _id: 0 } }
  4. ])
  • Instead of db.coll.distinct("x", { status: "A" }), use:
  1. db.coll.aggregate([
  2. { $match: { status: "A" } },
  3. { $group: { _id: null, distinctValues: { $addToSet: "$x" } } },
  4. { $project: { _id: 0 } }
  5. ])

The pipeline returns a cursor to a document:

  1. { "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 listIndexescommands and their helper methods are also excluded.

See also

Pending DDL Operations and Transactions