- MongoDB CRUD Operations >
- 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 Only replica sets using To ensure that a single thread can read its own writes, use
|
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.
For the level field, specify either the string "majority" or
"local".
The readConcern option is available for the following operations:
findcommandaggregatecommand and thedb.collection.aggregate()methoddistinctcommandcountcommandparallelCollectionScancommandgeoNearcommandgeoSearchcommand
To specify the read concern for the mongo shell method
db.collection.find(), use the cursor.readConcern()
method.