Read Concern

The readConcern option allows you to control the consistency andisolation properties of the data read from replica sets and replica setshards.

Through the effective use of write concerns and read concerns, you can adjust the levelof consistency and availability guarantees as appropriate, such aswaiting for stronger consistency guarantees, or loosening consistencyrequirements to provide higher availability.

MongoDB drivers updated for MongoDB 3.2 or later support specifyingread concern.

Read Concern Levels

The following read concern levels are available:

levelDescription
"local"The query returns data from the instance with no guarantee thatthe data has been written to a majority of the replica setmembers (i.e. may be rolled back).- Default for:- - reads against primary - reads against secondaries if the reads areassociated with causally consistent sessions.Read concern local is available for use withcausally consistent sessions and transactions.For more information, see the "local" referencepage.
"available"The query returns data from the instance with no guaranteethat the data has been written to a majority of the replica setmembers (i.e. may be rolled back).Default for reads against secondaries if the reads are notassociated with causally consistent sessions.For sharded collections, "available" read concernprovides the lowest latency reads possible among the variousread concerns but at the expense of consistency as"available" read concern can return orphaneddocuments.Read concern available is unavailable for usewith causally consistent sessions.For more information, see the "available"reference page.New in version 3.6.
"majority"The query returns the data that has been acknowledged by amajority of the replica set members. The documents returned bythe read operation are durable, even in the event of failure.To use read concern level of "majority", replicasets must use WiredTiger storage engine.You can disable read concern "majority" for a deploymentwith a three-member primary-secondary-arbiter (PSA) architecture;however, this has implications for change streams (in MongoDB 4.0 andearlier only) and transactions on sharded clusters. For more information,see Disable Read Concern Majority.Read concern majority is available for use withcausally consistent sessions and transactions.NoteFor operations in multi-document transactions, read concern "majority" providesits guarantees only if the transaction commits withwrite concern “majority”.Otherwise, the "majority" read concernprovides no guarantees about the data read in transactions.For more information, see the "majority"reference page.
"linearizable"The query returns data that reflects all successfulmajority-acknowledged writes that completed prior to the startof the read operation. The query may wait for concurrentlyexecuting writes to propagate to a majority of replicaset members before returning results.If a majority of your replica set members crash and restart afterthe read operation, documents returned by the read operation aredurable if writeConcernMajorityJournalDefault is set tothe default state of true.With writeConcernMajorityJournalDefault set to false,MongoDB does not wait for w: "majority"writes to be written to the on-disk journal before acknowledging thewrites. As such, majority write operations couldpossibly roll back in the event of a transient loss (e.g. crash andrestart) of a majority of nodes in a given replica set.You can specify linearizable read concern for read operations onthe primary only.Read concern linearizable is unavailable for usewith causally consistent sessions.You cannot use the $out or the $merge stagein conjunction with read concern "linearizable". Thatis, if you specify "linearizable" read concern fordb.collection.aggregate(), you cannot include eitherstages in the pipeline.Linearizable read concern guarantees only apply if readoperations specify a query filter that uniquely identifies asingle document.TipAlways use maxTimeMS with linearizable read concern incase a majority of data bearing members are unavailable.maxTimeMS ensures that the operation does not blockindefinitely and instead ensures that the operation returnsan error if the read concern cannot be fulfilled.For more information, see the "linearizable"reference page.
"snapshot"Only available for operations within multi-document transactions.- If the transaction is not part of a causally consistentsession, upon transaction commit with writeconcern "majority", the transactionoperations are guaranteed to have read from a snapshot ofmajority-committed data.- If the transaction is part of a causally consistentsession, upon transaction commit with writeconcern "majority", the transactionoperations are guaranteed to have read from a snapshot ofmajority-committed data that provides causal consistency withthe operation immediately preceding the transaction start.

Regardless of the read concern level, the most recent data on anode may not reflect the most recent version of the data in the system.

For more information on each read concern level, see:

readConcern Support

Read Concern Option

For operations not in multi-document transactions, you can specify a readConcern level as anoption to commands and methods that support read concern:

  1. readConcern: { level: <level> }

To specify the read concern level for the mongo shellmethod db.collection.find(), use thecursor.readConcern() method:

  1. db.collection.find().readConcern(<level>)

Transactions and Available Read Concerns

For multi-document transactions, you setthe read concern at the transaction level, not at the individualoperation level. The operations in the transaction will use thetransaction-level read concern. Any read concern set at the collectionand database level is ignored inside the transaction. If thetransaction-level read concern is explicitly specified, the clientlevel read concern is also ignored inside the transaction.

Important

Do not explicitly set the read concern for the individualoperations. To set the read concern for transactions, seeRead Concern/Write Concern/Read Preference.

You can set the read concern at the transaction start:

If unspecified at the transaction start, transactions use thesession-level read concern or, if that is unset, the client-level readconcern.

For more information, see Transaction Read Concern.

Causally Consistent Sessions and Available Read Concerns

For operations in a causally consistent session, "local" and"majority" levels are available. However, to guaranteecausal consistency, you must use "majority". Fordetails, see Causal Consistency.

If a multi-document transaction is associated with a causallyconsistent session, "snapshot" is also available for thethe transaction.

Operations That Support Read Concern

The following operations support read concern:

Important

To set read concern for operations in a transaction, you set theread concern at the transaction level, not at the individualoperation level. Do not explicitly set the read concern for theindividual operations in a transaction. For more information, seeTransactions and Read Concern.

Command/Method"local""available""majority""snapshot"[3]"linearizable"
count
distinct[2]
find
db.collection.find()viacursor.readConcern()
geoSearch
getMore
aggregatedb.collection.aggregate()[1]
Session.startTransaction()
[1]You cannot use the $out or the $merge stagein conjunction with read concern "linearizable". Thatis, if you specify "linearizable" read concern fordb.collection.aggregate(), you cannot include eitherstages in the pipeline.
[2]Read concern "snapshot" is available only formulti-document transactions. In a transaction, you cannot use thedistinct command or its helpers on a sharded collection.

The following write operations can also accept a read concern if partof a multi-document transaction:

Important

To set read concern for operations in a transaction, you set theread concern at the transaction level, not at the individualoperation level.

Command"local""available""majority""snapshot"[3]"linearizable"
deletedb.collection.deleteMany()db.collection.deleteOne()db.collection.remove()
findAndModifydb.collection.findAndModify()db.collection.findOneAndDelete()db.collection.findOneAndReplace()db.collection.findOneAndUpdate()
insertdb.collection.insert()db.collection.insertOne()db.collection.insertMany()
updatedb.collection.update()db.collection.updateMany()db.collection.updateOne()db.collection.replaceOne()
[3](1, 2) Read concern "snapshot" is available only formulti-document transactions, and for transactions, you set the readconcern at the transaction level. The operations that support"snapshot" correspond to the CRUD operationsavailable in transactions. For more information, seeTransactions and Read Concern.

Considerations

Read Your Own Writes

Changed in version 3.6.

Starting in MongoDB 3.6, you can use causally consistent sessions to read your own writes, if the writes requestacknowledgement.

Prior to MongoDB 3.6, you must have issued your write operation with{ w: "majority" } write concern and thenuse either "majority" or "linearizable"read concern for the read operations to ensure that a single thread canread its own writes.

Real Time Order

Combined with "majority" write concern,"linearizable" read concern enables multiple threads toperform reads and writes on a single document as if a single threadperformed these operations in real time; that is, the correspondingschedule for these reads and writes is considered linearizable.

Performance Comparisons

Unlike "majority", "linearizable" readconcern confirms with secondary members that the read operation isreading from a primary that is capable of confirming writes with{ w: "majority" } write concern.[4] As such, reads with linearizable readconcern may be significantly slower than reads with"majority" or "local" read concerns.

Always use maxTimeMS with linearizable read concern in case amajority of data bearing members are unavailable. maxTimeMS ensuresthat the operation does not block indefinitely and instead ensures thatthe operation returns an error if the read concern cannot be fulfilled.

For example:

  1. db.restaurants.find( { _id: 5 } ).readConcern("linearizable").maxTimeMS(10000)
  2.  
  3. db.runCommand( {
  4. find: "restaurants",
  5. filter: { _id: 5 },
  6. readConcern: { level: "linearizable" },
  7. maxTimeMS: 10000
  8. } )
[4]In some circumstances, two nodes in a replica setmay transiently believe that they are the primary, but at most, oneof them will be able to complete writes with { w:"majority" } write concern. The node that can complete{ w: "majority" } writes is the currentprimary, and the other node is a former primary that has not yetrecognized its demotion, typically due to a network partition.When this occurs, clients that connect to the former primary mayobserve stale data despite having requested read preferenceprimary, and new writes to the former primary willeventually roll back.

Read Operations and afterClusterTime

New in version 3.6.

MongoDB 3.6 introduces support for causally consistent sessions. For read operations associated with causally consistentsession, MongoDB 3.6 introduces the afterClusterTime read concernoption to be set automatically by the drivers for operations associatedwith causally consistent sessions.

Important

Do not manually set afterClusterTime for a read operation.MongoDB drivers set this value automatically for operationsassociated with causally consistent sessions. However, you canadvance the operation time and the cluster time for the session,such as to be consistent with the operations of another clientsession. For an example, see Examples.

To satisfy a read request with an afterClusterTime value of T,a mongod must perform the request after its oplogreaches time T. If its oplog has not reached time T, themongod must wait to service the request.

Read operations with a specified afterClusterTime return data thatmeet both the read concern levelrequirement and the specified afterClusterTime requirement.

For read operations not associated with causally consistent sessions,afterClusterTime is unset.