Shard a Time Series Collection分片某个时间系列集合
On this page本页内容
New in version 5.1. 5.1版新增。
Use this tutorial to shard a new or existing time series collection.使用本教程可以分割新的或现有的时间序列集合。
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分片一个新的时间序列集合
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.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 thetest
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分割现有的时间序列集合
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在deliverySensor
on thetest
database.test
数据库上共享一个名为deliverySensor
的现有时间序列集合。Specifies the将metadata.location
field as the shard key.metadata.location
字段指定为分片键。location
is a sub-field of the collection'smetaField
.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()
的集合是时间序列集合时,不需要指定时间序列选项。