v0.7.0
Menu

#Transactions

A transaction groups reads and writes on one database. Send begin, then ordinary requests on the same connection, then commit or abort.

{"cmd":"use","db":"shop"}
{"cmd":"begin"}
{"cmd":"insert","collection":"users","doc":{"name":"Ann"}}
{"cmd":"commit"}

The replies are {"began":true,"ok":true}, then the reply of each request, then {"committed":true,"ok":true,"ops":1} where ops is the number of writes. abort replies {"aborted":true,"ok":true}.

#What can be used inside

insert, update, delete, replace, findOneAndUpdate, findOneAndDelete, find, get, count, distinct and aggregate without $out or $merge. Each is checked as usual, one by one, with the same permissions.

Refused inside a transaction with bad_request, leaving the transaction open: createIndex, dropIndex, drop, compact, dropDatabase, use, watch, listCollections, listDatabases, the user commands, and $out and $merge. A second begin is also a bad_request, so there is one transaction at a time on a connection, and commit or abort without one is too.

#Isolation

The reads see the transaction's own writes, and nobody else sees them until the commit.

The first time a transaction uses a collection, the server copies that collection, indexes included, into a private working copy and records its version. The transaction works on the copies. abort, or closing the connection, throws them away.

A transaction holds no lock between requests.

#Commit and conflicts

The commit is optimistic. If any collection the transaction used was written to by someone else since the transaction first used it, the commit fails with "code":"conflict", and nothing is applied. Retry from begin.

  • A write by someone else counts even if it changed nothing, and so does the database being dropped.
  • Conflicts are per collection, not per document, so two transactions on different documents of one collection conflict.
  • Otherwise all the writes go into the log as one record, so after a crash the log has all of them or none. They are then published to change streams in order.

#When a transaction ends by itself

A failed write, a limit, or a collection that cannot be copied ends the transaction, and the reply has "aborted":true next to the error code. A duplicate key inside a transaction aborts it. A failed read does not end it.

{"aborted":true,"code":"invalid","error":"\"a.b\" is not allowed as a field name","ok":false}

A transaction is also aborted if the connection closes, or if it waits 30 seconds for its next request (a limit error that closes the connection).

#Limits

Limit Value
writes in one transaction 10,000
bytes of requests 64 MiB
documents in one collection that is copied 1,000,000
documents copied in all 2,000,000
collections used 100
idle time between requests 30 seconds

Going over one gives a limit error and ends the transaction. At the memory limit, a commit that adds anything is refused, and a commit of only deletes goes through.

#Costs to know about

  • The first use of a collection copies all of it. That is why there are document limits, and a transaction on a big collection is slow to start.
  • A pipeline that reads another collection with $lookup copies that one too.
  • Transactions are for one database. Nothing spans two.
  • A follower refuses transactions with read_only.