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.使用本教程可以分割新的或现有的时间序列集合。

Important重要

Before completing this tutorial, review the sharding limitations for time series collections.在完成本教程之前,请查看时间序列集合的分片限制

Prerequisites先决条件

To shard a time series collection, first:要分割时间序列集合,首先:

  1. Deploy a sharded cluster to host the database that contains your time series collection.部署一个分片集群来托管包含时间序列集合的数据库。
  2. Enable sharding for your database为数据库启用分片.

Procedure程序

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

1

Connect to your sharded cluster.连接到你的分片集群。

Connect mongosh to the mongos for your sharded cluster. mongosh连接到您的分片集群的mongosSpecify the host and port on which the mongos is running:指定运行mongoshostport

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

Create the collection.创建集合。

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()

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

Shard an Existing Time Series Collection分片现有的时间序列集合

1

Connect to your sharded cluster.连接到你的分片集群。

Connect mongosh to the mongos for your sharded cluster. mongosh连接到您的分片集群的mongosSpecify the host and port on which the mongos is running:指定运行mongoshostport

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

Shard the collection.将集合分片。

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()

  • 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()的集合是时间序列集合时,无需指定timeseries选项。

Additional Information其他信息

←  Build Materialized Views on Top of Time Series DataDocuments →