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

On this page本页内容

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:使用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.事务写入问题适用于提交操作和事务内的操作。

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相关读取操作使用"majority"读取关注点,并且
  • the associated write operations use "majority" write concern.相关的写入操作使用"majority"写入关注点。
←  Default MongoDB PortServer Sessions →