Docs HomeMongoDB Manual

Default MongoDB Read Concerns/Write Concerns默认MongoDB读取关注/写入关注

Read Concern读取关注

Read/Write Concern Inheritance

Default Read Concern默认读取关注

The default read concern is as follows:默认的读取关注如下:

Operations操作Default Read Concern默认读取关注
Reads against primary针对Primary的读取"local"
Note
  • This read concern can return data that may be rolled back.此读取关注可能会返回可能被回滚的数据。
  • This read concern does not guarantee causal consistency.此读取关注不能保证因果关系的一致性
Reads against secondaries.针对secondary读取。"local"
Note
  • This read concern can return data that may be rolled back.此读取关注可能会返回可能被回滚的数据。
  • This read concern does not guarantee causal consistency.此读取关注不能保证因果关系的一致性

Specify Read Concern: MongoDB Drivers指定读取关注:MongoDB驱动程序

Note

The following applies to operations issued outside transactions.以下内容适用于在事务之外发布的操作。

For read concern information related to operations issued inside transactions, click on the Operations in Transactions tab.有关事务内部发出的操作的已读取关注信息,请单击“事务中的操作”选项卡。

Using the MongoDB drivers, you can override the default read concern and set read concern for operations at the following levels:使用MongoDB驱动程序,您可以覆盖默认的读取关注,并为以下级别的操作设置读取关注:

Level级别Description描述
Client level客户端级别Applies to operations unless a finer-grained read concern for an operation is set at the database/collection/operation level.适用于操作,除非在数据库/集合/操作级别设置了操作的细粒度读取关注。
Database level数据库级别Applies to operations on the database's collections (i.e. overrides the client read concern) unless a read concern has been set at the collection level or the operation level.适用于数据库集合上的操作(即覆盖客户端读取关注),除非在集合级别或操作级别设置了读取关注。
Note

Does not apply to operations inside transactions.不适用于事务内部的操作。

Collection level集合级别Applies for read operations on the collection (i.e. overrides the database/client read concern) unless a read concern has been set at the operation level.适用于集合上的读取操作(即覆盖数据库/客户端读取关注),除非在操作级别设置了读取关注。
Note

Does not apply to operations inside transactions.不适用于事务内部的操作。

Operation level操作级别Applies for the specific read operation (i.e. overrides the database/client/collection read concern).适用于特定的读取操作(即覆盖数据库/客户端/集合读取关注)。
The ability to set read concern at the operation depends on the driver. Refer to your driver's documentation.操作时设置读取关注的能力取决于驾驶员。请参阅驱动程序文档
Note

Does not apply to operations inside transactions.不适用于事务内部的操作。

Note

The following applies to operations issued inside transactions.以下适用于在事务中发布的操作。

For read concern information related to operations issued outside transactions, click on the Operations outside Transactions tab.有关在事务外部发出的操作的已读取关注信息,请单击“事务外部的操作”选项卡。

Using the MongoDB drivers,you can override the default read concern and set read concern for transactions at the following levels:使用MongoDB驱动程序,您可以覆盖默认的读取关注,并为以下级别的事务设置读取关注:

Level数量Description描述
Client level客户端级别Applies to transactions unless a finer-grained read concern is set at the session/transaction level.适用于事务,除非在会话/事务级别设置了更细粒度的读取关注。
Note

All operations in a transaction use the transaction read concern; i.e., any read concern set at the operation/collection/database level is ignored inside the transaction.事务中的所有操作都使用事务读取关注;也就是说,在操作/集合/数据库级别设置的任何读取关注在事务中都会被忽略

Session level会话级别Applies to transactions started in the session (i.e. overrides the client read concern) unless a finer-grained read concern level is set at a specific transaction level.应用于会话中启动的事务(即覆盖客户端读取关注),除非在特定事务级别设置了更细粒度的读取关注级别。
Note

All operations in a transaction use the transaction read concern; i.e., any read concern set at the operation/collection/database level is ignored inside the transaction.事务中的所有操作都使用事务读取关注;也就是说,在操作/集合/数据库级别设置的任何读取关注在事务中都会被忽略。

See Transactions and Read Concern for more information.有关更多信息,请参阅事务和读取关注
Transaction level事务级别Applies to the specific transaction (i.e. overrides the client/session read concern).应用于特定事务(即覆盖客户端/会话读取关注)。
Note

All operations in a transaction use the transaction read concern; i.e., any read concern set at the operation/collection/database level is ignored inside the transaction.事务中的所有操作都使用事务读取关注;也就是说,在操作/集合/数据库级别设置的任何读取关注在事务中都会被忽略

See Transactions and Read Concern for more information.有关更多信息,请参阅事务和读取关注

Additional Information附加信息

For more information on the available read concerns, see Read Concern.有关可用读取关注的更多信息,请参阅读取关注

Write Concern撰写关注事项

Read/Write Concern Inheritance

Default Write Concern默认写入关注

Starting in MongoDB 5.0, the implicit default write concern is w: majority. 从MongoDB 5.0开始,隐含的默认写入关注w: majorityHowever, special considerations are made for deployments containing arbiters:但是,对于包含仲裁器的部署需要特别注意:

  • The voting majority of a replica set is 1 plus half the number of voting members, rounded down. 复制集的投票多数是1加上投票成员数量的一半,四舍五入。If the number of data-bearing voting members is not greater than the voting majority, the default write concern is { w: 1 }.如果带有数据的投票成员的数量不超过投票多数,则默认的写入关注为{ w: 1 }
  • In all other scenarios, the default write concern is { w: "majority" }.在所有其他场景中,默认的写入关注是{ w: "majority" }

Specifically, MongoDB uses the following formula to determine the default write concern:具体来说,MongoDB使用以下公式来确定默认的写入关注:

if [ (#arbiters > 0) AND (#non-arbiters <= majority(#voting-nodes)) ]
defaultWriteConcern = { w: 1 }
else
defaultWriteConcern = { w: "majority" }

For example, consider the following deployments and their respective default write concerns:例如,考虑以下部署及其各自的默认写入关注:

Non-Arbiters非仲裁器Arbiters仲裁器Voting Nodes投票节点Majority of Voting Nodes大多数投票节点Implicit Default Write Concern隐式默认写入关注
2132{ w: 1 }
4153{ w: "majority" }
  • In the first example:在第一个示例中:

    • There are 2 non-arbiters and 1 arbiter for a total of 3 voting nodes.对于总共3个投票节点,存在2个非仲裁器和1个仲裁器。
    • The majority of voting nodes (1 plus half of 3, rounded down) is 2.大多数投票节点(1加3的一半,四舍五入)为2。
    • The number of non-arbiters (2) is equal to the majority of voting nodes (2), resulting in an implicit write concern of { w: 1 }.非仲裁器的数量(2)等于大多数投票节点(2),导致{w:1}的隐式写入关注。
  • In the second example:在第二个示例中:

    • There are 4 non-arbiters and 1 arbiter for a total of 5 voting nodes.对于总共5个投票节点,有4个非仲裁器和1个仲裁器。
    • The majority of voting nodes (1 plus half of 5, rounded down) is 3.大多数投票节点(1加5的一半,四舍五入)为3。
    • The number of non-arbiters (4) is greater than the majority of voting nodes (3), resulting in an implicit write concern of { w: "majority" }.非仲裁器的数量(4)大于大多数投票节点(3),导致{ w: "majority" }的隐式写入关注。

Specify Write Concern: MongoDB Drivers指定写入关注:MongoDB驱动程序

Note

The following applies to operations issued outside transactions.以下内容适用于在事务之外发布的操作。

For read concern information related to operations issued inside transactions, click on the Operations in Transactions tab.有关事务内部发出的操作的已读取关注信息,请单击“事务中的操作”选项卡。

Using the MongoDB drivers, you can override the default write concern and set write concern for operations at the following levels:使用MongoDB驱动程序,您可以覆盖默认的写入关注,并为以下级别的操作设置写入关注:

Level数量Description描述
Client level客户端级别Applies to operations unless a finer-grained write concern for an operation is set at the operation/database/collection.适用于操作,除非在操作/数据库/集合中设置了操作的细粒度写入关注。
Database level数据库级别Applies to write operations on the database's collections (i.e. overrides the client write concern) unless a write concern has been set at the collection level or the operation level.适用于数据库集合上的写入操作(即重写客户端写入关注),除非在集合级别或操作级别设置了写入关注。
Note

Does not apply to operations inside transactions.不适用于事务内部的操作。

Collection level集合级别Applies to write operations on the collection (i.e. overrides the database and client write concern) unless a write concern has been set at the operation level.适用于对集合的写操作(即重写数据库和客户端写入关注),除非在操作级别设置了写入关注。
Note

Does not apply to operations inside transactions.不适用于事务内部的操作。

Operation level操作级别Applies to the specific write operation.适用于特定的写入操作。

The ability to set write concern at the operation depends on the driver. Refer to your driver's documentation.在操作中设置写入关注的能力取决于驱动程序。请参阅驱动程序文档

Note

Does not apply to operations inside transactions.不适用于事务内部的操作。

Note

The following applies to operations issued inside transactions.

For read concern information related to operations issued outside transactions, click on the Operations outside Transactions tab.

Using the MongoDB drivers, you can override the default write concern and set write concern for for transactions at the following levels:

LevelDescription描述
Client level客户端级别Applies to transactions unless a finer-grained write concern for transactions are set at the session/transaction level.适用于事务,除非在会话/事务级别设置了事务的细粒度写入关注。
Transaction write concern applies to the commit operation and the operations inside the transaction.事务写入关注适用于提交操作和事务内部的操作。
Note

All operations within a transaction use the transaction write concern; i.e., any write concern set at the operation/collection/database level is ignored inside the transaction.事务中的所有操作都使用事务写入关注;即,在事务中忽略在操作/集合/数据库级别设置的任何写入关注。

Session level会话级别Applies for transactions started in the session unless the write concern level is set at a specific transaction level.适用于会话中启动的事务,除非写入关注级别设置为特定事务级别。
Transaction write concern applies to the commit operation and the operations inside the transaction.事务写入关注适用于提交操作和事务内部的操作。
Note

All operations within a transaction use the transaction write concern; i.e., any write concern set at the operation/collection/database level is ignored inside the transaction.事务中的所有操作都使用事务写入关注;即,在事务中忽略在操作/集合/数据库级别设置的任何写入关注。

Transaction level事务级别Applies to the specific transaction.适用于特定事务。
Transaction write concern applies to the commit operation and the operations inside the transaction.事务写入关注适用于提交操作和事务内部的操作。
Note

All operations within a transaction use the transaction write concern; i.e., any write concern set at the operation/collection/database level is ignored inside the transaction.事务中的所有操作都使用事务写入关注;即,在事务中忽略在操作/集合/数据库级别设置的任何写入关注。

See Transactions and Write Concern for more information.

Additional Information附加信息

For more information on the available write concerns, see Write Concern.

Causally Consistency Guarantees因果一致性保证

With causally consistent client sessions, the client sessions only guarantee causal consistency if:

  • the associated read operations use "majority" read concern, and
  • the associated write operations use "majority" write concern.