On this page本页内容
Session.
startTransaction
(<options>)¶New in version 4.0.版本4.0中的新功能。
Starts a multi-document transaction associated with the session. At any given time, you can have at most one open transaction for a session.
Availability
Important
Within a transaction, you can only specify read and write (CRUD) operations on existing collections. For example, a multi-document transaction cannot include an insert operation that would result in the creation of a new collection.
The Session.startTransaction()
method can take a document following options:
Option | |
---|---|
readConcern |
Optional. A document that specifies the read concern for all operations in the transaction, overriding operation-specific read concern. You can specify one of the following read concern levels: For |
writeConcern |
Optional. A document that specifies the write concern for the transaction. This write concern applies to the transaction commit and abort operations. The operations within the transaction use If you commit using For MongoDB Drivers, transactions use the client-level write concern as the default. |
Note
If running with access control, you must have privileges for the operations in the transaction.
For multi-document transactions:
"4.4"
or greater, you can create collections and indexes in transactions. For details, see Create Collections and Indexes In a TransactionNote
You cannot create new collections in cross-shard write transactions. For example, if you write to an existing collection in one shard and implicitly create a collection in a different shard, MongoDB cannot perform both operations in the same transaction.
config
, admin
, or local
databases.system.*
collections.explain
).getMore
inside the transaction.getMore
outside the transaction.killCursors
as the first operation in a transaction.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 feature compatibility version (fcv) For fcv
| |
insert |
For feature compatibility version (fcv) For fcv
| |
db.collection.save() |
For feature compatibility version (fcv) With fcv
| |
update |
For feature compatibility version (fcv) For fcv
| |
For feature compatibility version (fcv) For fcv
|
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.
Informational commands, such as isMaster
, buildInfo
, connectionStatus
(and their helper methods) are allowed in transactions; however, they cannot be the first operation in the transaction.
Transactions support read preference primary
.
While the transaction is open, no data changes made by operations in the transaction is visible outside the transaction:
Until a transaction commits, the data changes made in the transaction are not visible outside the transaction.
However, when a transaction writes to multiple shards, not all outside read operations need to wait for the result of the committed transaction to be visible across the shards. For example, if a transaction is committed and write 1 is visible on shard A but write 2 is not yet visible on shard B, an outside read at read concern "local"
can read the results of write 1 without seeing write 2.
Consider a scenario where as changes are made to an employee’s record in the hr
database, you want to ensure that the events
collection in the reporting
database are in sync with the hr
changes. That is, you want to ensure that these writes are done as a single transaction, such that either both operations succeed or fail.
The employees
collection in the hr
database has the following documents:
The events
collection in the reporting
database has the following documents:
The following example opens a transaction, updates an employee’s status to Inactive
in the employees
status and inserts a corresponding document to the events
collection, and commits the two operations as a single transaction.