Use this tutorial to shard a new or existing time series collection.使用本教程对新的或现有的时间序列集合进行分片。
Important
Before completing this tutorial, review the sharding limitations for time series collections.在完成本教程之前,请查看时间序列集合的分片限制。
Prerequisites先决条件
To shard a time series collection, you must deploy a sharded cluster to host the database that contains your time series collection.要对时间序列集合进行分片,您必须部署一个分片集群来托管包含时间序列集合的数据库。
Note
Starting in MongoDB 8.0.10, you can reshard a time series collection. All shards in the time series collection must run version 8.0.10 or later to reshard.从MongoDB 8.0.10开始,您可以重新标记时间序列集合。时间序列集合中的所有分片必须运行8.0.10或更高版本才能重新分级。
Procedures过程
Create a Sharded Time Series Collection创建分片时间序列集合
Connect to your sharded cluster.连接到分片集群。
Connect 将mongosh to the mongos for your sharded cluster. Specify the host and port on which the mongos is running:mongosh连接到分片集群的mongos。指定运行mongos的host和port:
mongosh --host <hostname> --port <port>Confirm that sharding is enabled on your database.确认数据库已启用分片。
Run 运行sh.status() to confirm that sharding is enabled on your database:sh.status()以确认数据库上已启用分片:
sh.status()
The command returns the sharding information:该命令返回分片信息:
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
...Create the collection.创建集合。
Use the 使用带有shardCollection() method with the timeseries option.timeseries选项的shardCollection()方法。
For example:例如:
sh.shardCollection(
"test.weather",
{ "metadata.sensorId": 1 },
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "hours"
}
}
)
In this example, 在这个例子中,sh.shardCollection():sh.shardCollection():
Shards a new time series collection named在weatheron thetestdatabase.test数据库上创建了一个名为weather的新时间序列集合。Specifies the将metadata.sensorIdfield as the shard key.metadata.sensorId字段指定为分片键。Specifies a指定小时的granularityof hours.granularity。
The following document contains the appropriate metadata for the collection:以下文档包含该集合的适当元数据:
db.weather.insertOne( {
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T00:00:00.000Z"),
"temp": 12
} )Connect to your sharded cluster.连接到分片集群。
Connect 将mongosh to the mongos for your sharded cluster. Specify the host and port on which the mongos is running:mongosh连接到分片集群的mongos。指定运行mongos的主机和端口:
mongosh --host <hostname> --port <port>Confirm that sharding is enabled on your database.确认数据库已启用分片。
Run 运行sh.status() to confirm that sharding is enabled on your database:sh.status()以确认数据库上已启用分片:
sh.status()
The command returns the sharding information:该命令返回分片信息:
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
...Create the collection.创建集合。
Use the 使用带有shardCollection() method with the timeseries option.timeseries选项的shardCollection()方法。
For example:例如:
sh.shardCollection(
"test.weather",
{ "metadata.sensorId": 1 },
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "hours"
}
}
)
In this example, 在这个例子中,sh.shardCollection():sh.shardCollection():
Shards a new time series collection named在weatheron thetestdatabase.test数据库上创建了一个名为weather的新时间序列集合。Specifies the将metadata.sensorIdfield as the shard key.metadata.sensorId字段指定为分片键。Specifies a指定小时的granularityof hours.granularity。
The following document contains the appropriate metadata for the collection:以下文档包含该集合的适当元数据:
db.weather.insertOne( {
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T00:00:00.000Z"),
"temp": 12
} )Shard an Existing Time Series Collection对现有时间序列集合进行分片化
Connect to your sharded cluster.连接到分片集群。
Connect 将mongosh to the mongos for your sharded cluster. Specify the host and port on which the mongos is running:mongosh连接到分片集群的mongos。指定运行mongos的主机和端口:
mongosh --host <hostname> --port <port>Confirm that sharding is enabled on your database.确认数据库已启用分片。
Run 运行sh.status() to confirm that sharding is enabled on your database:sh.status()以确认数据库上已启用分片:
sh.status()
The command returns the sharding information:该命令返回分片信息:
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
...Create a hashed index on your collection.在集合上创建哈希索引。
Enable sharding on your collection by creating an index that supports the shard key.通过创建支持分片键的索引,在集合上启用分片。
Consider a time series collection with the following properties:考虑具有以下属性的时间序列集合:
db.createCollection(
"deliverySensor",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "minutes"
}
}
)
A sample document from the collection resembles:集合中的示例文档类似于:
db.deliverySensor.insertOne( {
"metadata": { "location": "USA", "vehicle": "truck" },
"timestamp": ISODate("2021-08-21T00:00:10.000Z"),
"speed": 50
} )
Run the following command to create a hashed index on the 运行以下命令在metadata.location field:metadata.location字段上创建哈希索引:
db.deliverySensor.createIndex( { "metadata.location" : "hashed" } )Shard your collection.分割了集合。
Use the 使用shardCollection() method to shard the collection.shardCollection()方法对集合进行分片。
To shard the 要对上一步中描述的deliverySensor collection described in the preceding step, run the following command:deliverySensor集合进行分片,请运行以下命令:
sh.shardCollection( "test.deliverySensor", { "metadata.location": "hashed" } )
In this example, 在这个例子中,sh.shardCollection():sh.shardCollection():
Shards an existing time series collection named在deliverySensoron thetestdatabase.test数据库上分片一个名为deliverySensor的现有时间序列集合。Specifies the将metadata.locationfield as the shard key.locationis a sub-field of the collection'smetaField.metadata.location字段指定为分片键。location是集合的metaField的子字段。
When the collection you specify to 当您为sh.shardCollection() is a time series collection, you do not need to specify the timeseries option.sh.shardCollection()指定的集合是时间序列集合时,您不需要指定timeseries选项。