Sharding
MongoDB 通过分片支持大型数据集,而分片是一种将数据分布到多个数据库服务器的方法。请参阅 MongoDB Documentation 以了解如何设置分片集群及其要求和限制。
MongoDB supports large data sets via sharding, a method for distributing data across multiple database servers. Please refer to the MongoDB Documentation to learn how to set up a sharded cluster, its requirements and limitations.
Spring Data MongoDB 使用 @Sharded
注释标识存储在分片集合中的实体,如下所示。
Spring Data MongoDB uses the @Sharded
annotation to identify entities stored in sharded collections as shown below.
@Document("users")
@Sharded(shardKey = { "country", "userId" }) 1
public class User {
@Id
Long id;
@Field("userid")
String userId;
String country;
}
1 | The properties of the shard key get mapped to the actual field names. |
Sharded Collections
Spring Data MongoDB 不会自动为集合及其所需的索引设置分片。以下代码段展示了如何使用 MongoDB 客户端 API 执行此操作。
Spring Data MongoDB does not auto set up sharding for collections nor indexes required for it. The snippet below shows how to do so using the MongoDB client API.
MongoDatabase adminDB = template.getMongoDbFactory()
.getMongoDatabase("admin"); 1
adminDB.runCommand(new Document("enableSharding", "db")); 2
Document shardCmd = new Document("shardCollection", "db.users") 3
.append("key", new Document("country", 1).append("userid", 1)); 4
adminDB.runCommand(shardCmd);
1 | Sharding commands need to be run against the admin database. |
2 | Enable sharding for a specific database if necessary. |
3 | Shard a collection within the database having sharding enabled. |
4 | Specify the shard key. This example uses range based sharding. |
Shard Key Handling
分片键由目标集合中每个文档都必须存在的单个或多个属性组成。它用于在分片之间分布文档。
The shard key consists of a single or multiple properties that must exist in every document in the target collection. It is used to distribute documents across shards.
将 @Sharded
注释添加到实体可使 Spring Data MongoDB 对分片方案所需的最佳尝试优化生效。从本质上讲,这意味着在 upsert 实体时,如果尚未存在,则向 replaceOne
筛选器查询添加所需的分片键信息。这可能需要进行额外的服务器往返,以确定当前分片键的实际值。
Adding the @Sharded
annotation to an entity enables Spring Data MongoDB to apply best effort optimisations required for sharded scenarios.
This means essentially adding required shard key information, if not already present, to replaceOne
filter queries when upserting entities.
This may require an additional server round trip to determine the actual value of the current shard key.
通过设置 |
By setting |
有关详细信息,请参见 MongoDB Documentation。下列列表包含符合分片键自动包含的操作:
Please see the MongoDB Documentation for further details. The following list contains which operations are eligible for shard key auto-inclusion:
-
(Reactive)CrudRepository.save(…)
-
(Reactive)CrudRepository.saveAll(…)
-
(Reactive)MongoTemplate.save(…)