On this page本页内容
By default, clients reads from a replica set's primary; however, clients can specify a read preference to direct read operations to other members. 默认情况下,客户端从副本集的primary读取数据;但是,客户端可以指定读取首选项,以便将读取操作定向到其他成员。For example, clients can configure read preferences to read from secondaries or from nearest member to:例如,客户端可以将读取首选项配置为从第二个成员或最近的成员读取到:
Read operations from secondary members of replica sets may not reflect the current state of the primary. 来自副本集辅助成员的读取操作可能不会反映主副本集的当前状态。Read preferences that direct read operations to different servers may result in non-monotonic reads.将读取操作定向到不同服务器的读取首选项可能会导致非单调读取。
Changed in version 3.6.在版本3.6中更改。
You can configure the read preference on a per-connection or per-operation basis. 您可以根据每个连接或每个操作配置读取首选项。For more information on read preference or on the read preference modes, see Read Preference and Read Preference Modes.有关读取首选项或读取首选项模式的更多信息,请参阅读取首选项和读取首选项模式。
In replica sets, all write operations go to the set's primary. 在副本集中,所有写入操作都会转到副本集的primary。The primary applies the write operation and records the operations on the primary's operation log or oplog. primary应用写操作,并将操作记录在主的操作日志或oplog上。The oplog is a reproducible sequence of operations to the data set. oplog是数据集的一个可复制的操作序列。secondary members of the set continuously replicate the oplog and apply the operations to themselves in an asynchronous process.集合的secondary成员持续复制oplog,并在异步过程中将操作应用于自己。
For more information on replica sets and write operations, see Replication and Write Concern.有关副本集和写入操作的更多信息,请参阅副本和写入关注。
Sharded clusters allow you to partition a data set among a cluster of 分片集群允许您以对应用程序几乎透明的方式在mongod
instances in a way that is nearly transparent to the application. mongod
实例集群中划分数据集。For an overview of sharded clusters, see the Sharding section of this manual.有关分片集群的概述,请参阅本手册的分片部分。
For a sharded cluster, applications issue operations to one of the 对于分片集群,应用程序向与集群关联的一个mongos
instances associated with the cluster.mongos
实例发出操作。
Read operations on sharded clusters are most efficient when directed to a specific shard. 分片集群上的读取操作在定向到特定分片时效率最高。Queries to sharded collections should include the collection's shard key. 对分片集合的查询应包括集合的分片键。When a query includes a shard key, the 当查询包含分片键时,mongos
can use cluster metadata from the config database to route the queries to shards.mongos
可以使用配置数据库中的集群元数据将查询路由到分片。
If a query does not include the shard key, the 如果查询不包括分片键,mongos
must direct the query to all shards in the cluster. mongos
必须将查询指向集群中的所有分片。These scatter gather queries can be inefficient. 这些分散-聚集查询可能效率低下。On larger clusters, scatter gather queries are unfeasible for routine operations.在较大的集群上,分散-聚集查询对于常规操作是不可行的。
For replica set shards, read operations from secondary members of replica sets may not reflect the current state of the primary. 对于副本集分片,来自副本集次要成员的读取操作可能不会反映主要成员的当前状态。Read preferences that direct read operations to different servers may result in non-monotonic reads.将读取操作定向到不同服务器的读取首选项可能会导致非单调读取。
Starting in MongoDB 3.6,从MongoDB 3.6开始,
"available"
. "available"
,这将防止从二级读取返回孤立数据。For more information on read operations in sharded clusters, see the mongos and Shard Keys sections.有关分片集群中读取操作的更多信息,请参阅mongos
和分片键部分。
For sharded collections in a sharded cluster, the 对于分片集群中的分片集合,mongos
directs write operations from applications to the shards that are responsible for the specific portion of the data set. mongos
将写操作从应用程序引导到负责数据集特定部分的分片。The mongos
uses the cluster metadata from the config database to route the write operation to the appropriate shards.mongos
使用配置数据库中的集群元数据将写操作路由到适当的分片。
MongoDB partitions data in a sharded collection into ranges based on the values of the shard key. MongoDB根据分片键的值将分片集合中的数据划分为多个范围。Then, MongoDB distributes these chunks to shards. 然后,MongoDB将这些块分配给分片。The shard key determines the distribution of chunks to shards. 分片键决定了块到分片的分布。This can affect the performance of write operations in the cluster.这可能会影响群集中写入操作的性能。
If the value of the shard key increases or decreases with every insert, all insert operations target a single shard. 如果分片键的值随着每次插入而增加或减少,则所有插入操作都以单个分片为目标。As a result, the capacity of a single shard becomes the limit for the insert capacity of the sharded cluster.因此,单个分片的容量成为分片集群插入容量的限制。
For more information, see Sharding and Bulk Write Operations.有关详细信息,请参阅分片和批量写入操作。