- Introduction to MongoDB >
- Databases and Collections >
- Time Series Collections >
- Shard a Time Series Collection
Shard a Time Series Collection¶
On this page
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.
Prerequisites¶
To shard a time series collection, first:
- Deploy a sharded cluster to host the database that contains your time series collection.
Procedure¶
Shard a New Time Series Collection¶
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:
Confirm that sharding is enabled on your database.¶
Run sh.status() to confirm that sharding is enabled on your database:
The command returns the sharding information:
Create the collection.¶
Use the shardCollection() method with the timeseries option.
For example:
In this example, sh.shardCollection():
- Shards a new time series collection named
weatheron thetestdatabase. - Specifies the
metadata.sensorIdfield as the shard key. - Specifies a
granularityof hours.
The following document contains the appropriate metadata for the collection:
Shard an Existing Time Series Collection¶
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:
Confirm that sharding is enabled on your database.¶
Run sh.status() to confirm that sharding is enabled on your database:
The command returns the sharding information:
Shard the collection.¶
Use the shardCollection() method to shard the
collection.
Consider a time series collection with the following properties:
A sample document from the collection resembles:
To shard the collection, run the following command:
In this example, sh.shardCollection():
- Shards an existing time series collection named
deliverySensoron thetestdatabase. - Specifies the
metadata.locationfield as the shard key.locationis a sub-field of the collection’smetaField.
When the collection you specify to sh.shardCollection() is
a time series collection, you do not need to specify the
timeseries
option.