Database Manual / Reference

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"

  • This read concern can return data that may be rolled back.此读取关注可能会返回可能被回滚的数据。
  • This read concern does not guarantee causal consistency.这种读取关注并不能保证因果关系的一致性
Reads against secondaries.针对secondary的读取。

"local"

  • 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驱动程序

Operations outside Transactions事务之外的操作

The following information applies to operations that are run outside transactions. For read concern information related to operations that are run inside transactions, click 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.适用于数据库集合上的操作(即覆盖客户端读取关注),除非在集合级别或操作级别设置了读取关注。

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.适用于对集合的读取操作(即覆盖数据库/客户端读取关注),除非在操作级别设置了读取关注。

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.在操作时设置读取关注的能力取决于驾驶员。请参阅驱动程序文档

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

Operations in Transactions事务中的操作

The following information applies to operations that are run inside transactions. For read concern information related to operations that are run outside transactions, click 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.适用于事务,除非在会话/事务级别设置了更细粒度的读取关注。

All operations within a transaction use the transaction read concern: Any read concern set at the operation, collection, or 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.适用于会话中启动的事务(即覆盖客户端读取关注),除非在特定事务级别设置了更细粒度的读取关注级别。

All operations within a transaction use the transaction read concern: Any read concern set at the operation, collection, or 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).适用于特定事务(即覆盖客户端/会话读取关注)。

All operations within a transaction use the transaction read concern: Any read concern set at the operation, collection, or 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默认写入关注

The implicit default write concern is w: majority. 隐含的默认写入关注w: majorityw: majority ensures write durability by requiring replica sets to wait for on-disk journaling by default, controlled by writeConcernMajorityJournalDefault. 默认情况下,通过要求副本集等待磁盘上的日志记录来确保写入持久性,该日志记录由writeConcernMajorityJournalDefault控制。However, there is an edge case for replica set deployments containing arbiters:但是,包含仲裁器的副本集部署存在一个边缘情况:

  • The voting majority of a replica set is 1 plus half the number of voting members, rounded down. If the number of data-bearing voting members is not greater than the voting majority, the default write concern is { w: 1 }.复制品集的投票多数为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驱动程序

Operations outside Transactions事务之外的操作

The following information applies to operations that are run outside transactions. 以下信息适用于在事务外部运行的操作。For read concern information related to operations that are run inside transactions, click 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 levelApplies 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适用于数据库集合上的写操作(即覆盖客户端写关注),除非在集合级别或操作级别设置了写关注.

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.适用于对集合的写入操作(即覆盖数据库和客户端写入关注),除非在操作级别设置了写入关注。

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.在操作中设置写入关注的能力取决于驱动程序。请参阅驱动程序文档

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

Operations in Transactions事务中的操作

The following information applies to operations that are run inside transactions. 以下信息适用于在事务内部运行的操作。For read concern information related to operations that are run outside transactions, click 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:使用MongoDB驱动程序,您可以覆盖默认的写入关注,并在以下级别为事务设置写入关注:

Level级别Description描述
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.事务写入关注适用于提交操作和事务内部的操作。

All operations within a transaction use the transaction write concern: Any write concern set at the operation, collection, or 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.事务写入关注适用于提交操作和事务内部的操作。

All operations within a transaction use the transaction write concern: Any write concern set at the operation, collection, or 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.事务写入关注适用于提交操作和事务内部的操作。

All operations within a transaction use the transaction write concern: Any write concern set at the operation, collection, or 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.有关可用写入关注的更多信息,请参阅写入关注

Causal Consistency Guarantees因果一致性保证

With causally consistent client sessions, the client sessions only guarantee causal consistency if:对于因果一致的客户端会话,客户端会话仅在以下情况下保证因果一致性

  • the associated read operations use "majority" read concern, and相关的读取操作使用"majority"读取关注,以及
  • the associated write operations use "majority" write concern.相关的写入操作使用"majority"写入关注。