Docs Menu

Shard a Time Series Collection

On this page

  • Prerequisites
  • Procedure
  • Additional Information

New in version 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.

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.
1

Connect mongosh to the mongos for your sharded cluster. Specify the host and port on which the mongos is running:

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

Run sh.status() to confirm that sharding is enabled on your database:

sh.status()

The command returns the sharding information:

--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
...

If sh.status() returns This db does not have sharding enabled., enable sharding.

3

Use the shardCollection() method with the timeseries option.

For example:

sh.shardCollection(
"test.weather",
{ "metadata.sensorId": 1 },
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "hours"
}
}
)

In this example, sh.shardCollection():

  • Shards a new time series collection named weather on the test database.
  • Specifies the metadata.sensorId field as the shard key.
  • Specifies a granularity of hours.

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
} )
1

Connect mongosh to the mongos for your sharded cluster. Specify the host and port on which the mongos is running:

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

Run sh.status() to confirm that sharding is enabled on your database:

sh.status()

The command returns the sharding information:

--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
...

If sh.status() returns This db does not have sharding enabled., enable sharding.

3

Use the shardCollection() method to shard the collection.

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

  • Shards an existing time series collection named deliverySensor on the test database.
  • Specifies the metadata.location field as the shard key. location is a sub-field of the collection's metaField.

When the collection you specify to sh.shardCollection() is a time series collection, you do not need to specify the timeseries option.

←  Build Materialized Views on Top of Time Series DataDocuments →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.