- MongoDB CRUD Operations >
- MongoDB CRUD Reference >
- Read Concern
Read Concern¶
New in version 3.2.
MongoDB 3.2 introduces the readConcern query option for replica sets and replica set shards. By default, MongoDB uses a read concern of "local" to return the most recent data available to the MongoDB instance at the time of the query, even if the data has not been persisted to a majority of replica set members and may be rolled back.
Storage Engine and Drivers Support¶
For the WiredTiger storage engine, the readConcern option allows clients to choose a level of isolation for their reads. You can specify a read concern of "majority" to read data that has been written to a majority of replica set members and thus cannot be rolled back.
With the MMAPv1 storage engine, you can only specify a readConcern option of "local".
Tip
The serverStatus command returns the storageEngine.supportsCommittedReads field which indicates whether the storage engine supports "majority" read concern.
readConcern requires MongoDB drivers updated for 3.2.
Read Concern Levels¶
By default, MongoDB uses a readConcern of "local" which does not guarantee that the read data would not be rolled back.
You can specify a readConcern of "majority" to read data that has been written to a majority of replica set members and thus cannot be rolled back.
level | Description |
---|---|
|
Default. The query returns the instance’s most recent copy of data. Provides no guarantee that the data has been written to a majority of the replica set members. |
|
The query returns the instance’s most recent copy of data confirmed as written to a majority of members in the replica set. To use a read concern level of "majority", you must use the WiredTiger storage engine and start the mongod instances with the --enableMajorityReadConcern command line option (or the replication.enableMajorityReadConcern setting if using a configuration file). Only replica sets using protocol version 1 support "majority" read concern. Replica sets running protocol version 0 do not support "majority" read concern. To ensure that a single thread can read its own writes, use "majority" read concern and "majority" write concern against the primary of the replica set. |
Regardless of the read concern level, the most recent data on a node may not reflect the most recent version of the data in the system.
readConcern Option¶
Use the readConcern option to specify the read concern level.
readConcern: { level: <"majority"|"local"> }
For the level field, specify either the string "majority" or "local".
The readConcern option is available for the following operations:
- find command
- aggregate command and the db.collection.aggregate() method
- distinct command
- count command
- parallelCollectionScan command
- geoNear command
- geoSearch command
To specify the read concern for the mongo shell method db.collection.find(), use the cursor.readConcern() method.
Thank you for your feedback!
We're sorry! You can Report a Problem to help us improve this page.