Docs HomeMongoDB Manual

Shard a Time Series Collection分片某个时间系列集合

New in version 5.1. 5.1版新增。

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.在完成本教程之前,请查看时间序列集合的分片限制

Limitations局限性

You can't reshard a sharded time series collection. 无法重新分片已分片的时间序列集合。However, you can refine its shard key.但是,您可以细化其分片关键帧

Prerequisites先决条件

To shard a time series collection, you must deploy a sharded cluster to host the database that contains your time series collection.要对时间序列集合进行分片,必须部署一个分片集群来承载包含时间序列集合的数据库。

Procedures过程

Shard a New Time Series Collection分片一个新的时间序列集合

1

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。指定运行mongoshostport

mongosh --host <hostname> --port <port>
2

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,
...
3

Create the collection.创建集合。

Use the shardCollection() method with the timeseries option.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 weather on the test database.test数据库上共享一个名为weather的新时间序列集合。
  • Specifies the metadata.sensorId field as the shard key.指定metadata.sensorId字段作为分片键
  • Specifies a granularity of 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分割现有的时间序列集合

1

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。指定运行mongoshostport

mongosh --host <hostname> --port <port>
2

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,
...
3

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" } )
4

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 deliverySensor on the test database.test数据库上共享一个名为deliverySensor的现有时间序列集合。
  • Specifies the metadata.location field as the shard key. metadata.location字段指定为分片键location is a sub-field of the collection's metaField.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()的集合是时间序列集合时,不需要指定时间序列选项。

Additional Information附加信息