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.在完成本教程之前,请查看时间序列集合的分片限制。
To shard a time series collection, first:要分割时间序列集合,首先:
Use the 将shardCollection() method with the timeseries option.shardCollection()方法与timeseries选项一起使用。
For example:例如:
sh.shardCollection( "test.weather", { "metadata.sensorId": 1 }, { timeseries: { timeField: "timestamp", metaField: "metadata", granularity: "hours" } } )
In this example, 在本例中,sh.shardCollection():sh.shardCollection():
weather on the test database.test数据库中分片一个名为weather的新时间序列集合。metadata.sensorId field as the shard key.metadata.sensorId字段指定为分片键。granularity of hours.granularity为"hour"。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
} )
Use the 使用shardCollection() method to shard the collection.shardCollection()方法对集合进行分片。
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
} )
To shard the collection, run the following command:要分割集合,请运行以下命令:
sh.shardCollection( "test.deliverySensor", { "metadata.location": 1 } )
In this example, 在本例中,sh.shardCollection():sh.shardCollection():
deliverySensor on the test database.test测试数据库中名为deliverySensor的现有时间序列集合。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()的集合是时间序列集合时,无需指定timeseries选项。