Default MongoDB Read Concerns/Write Concerns默认MongoDB读取关注/写入关注
On this page本页内容
Read Concern读取关注
Default Read Concern默认读取关注
The default read concern is as follows:默认的读取关注如下:
"local"
Note
| |
"local"
Note
|
Specify Read Concern: MongoDB Drivers指定读取关注:MongoDB驱动程序
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驱动程序,您可以覆盖默认的读取关注,并为以下级别的操作设置读取关注:
Note
| |
Note
| |
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驱动程序,您可以覆盖默认的读取关注,并为以下级别的事务设置读取关注:
Note
| |
Note
| |
Note
|
Additional Information附加信息
For more information on the available read concerns, see Read Concern.有关可用读取关注的更多信息,请参阅读取关注。
Write Concern撰写关注事项
Default Write Concern默认写入关注
Starting in MongoDB 5.0, the implicit default write concern is 从MongoDB 5.0开始,隐含的默认写入关注是w: majority
. w: majority
。However, 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:例如,考虑以下部署及其各自的默认写入关注:
2 | 1 | 3 | 2 | { w: 1 } |
4 | 1 | 5 | 3 | { 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非仲裁器的数量(2)等于大多数投票节点(2),导致{ w: 1 }
.{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非仲裁器的数量(4)大于大多数投票节点(3),导致{ w: "majority" }
.{ w: "majority" }
的隐式写入关注。
Specify Write Concern: MongoDB Drivers指定写入关注:MongoDB驱动程序
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驱动程序,您可以覆盖默认的写入关注,并为以下级别的操作设置写入关注:
Note
| |
Note
| |
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:
Level | |
---|---|
Note
| |
Note
| |
Note
|
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.