Time Series Quick Start
This quick start describes how to configure, create, and query a time series collection with MongoDB Atlas or a self-managed deployment.
Time required: 30 minutes
Steps
Set up your Atlas cluster.
Create a free Atlas account or sign in to an existing account.
If you don't yet have an Atlas cluster, create a free M0 cluster. To learn more about creating an Atlas cluster, see Create a Cluster.
Note
If you are working with an existing cluster, you must have
Project Data Access Admin
or higher access to your Atlas project.If you create a new cluster, you have the necessary permissions by default.
You can create only one
M0
Free cluster per project.In the left sidebar, click Overview. Choose your cluster and click Connect.
Under Access your data through tools, click Shell.
If you haven't already, follow the steps provided to download and install
mongosh
.Copy your connection string and click Done.
Open a new terminal window and connect to your deployment.
Use mongosh
to connect to your self-managed or Atlas deployment.
For example:
mongosh "mongodb+srv://my-test-cluster.1twap.mongodb.net/" --apiVersion 1 --username <user>
Create an empty time series collection.
Note
This exercise uses stock ticker sample data. The date
field stores time data, and
the ticker
field identifies the individual stock.
Set the
timeField
,metaField
, andgranularity
:timeseries: { timeField: "date", metaField: "ticker", granularity: "seconds" } OR specify custom granularity:
New in version 6.3.
timeseries: { timeField: "date", metaField: "ticker", granularity: "seconds", bucketMaxSpanSeconds: "300", bucketRoundingSeconds: "300" } Create the collection using the
db.createCollection()
method:db.createCollection( "stocks", { timeseries: { timeField: "date", metaField: "ticker", granularity: "seconds" } }) This creates an empty time series collection named
stocks
.
Add sample documents.
Run the db.collection.insertMany()
method to add the
following sample documents to the collection:
db.stocks.insertMany([ { ticker: "MDB", date: ISODate("2021-12-18T15:59:00.000Z"), close: 252.47, volume: 55046.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:58:00.000Z"), close: 252.93, volume: 44042.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:57:00.000Z"), close: 253.61, volume: 40182.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:56:00.000Z"), close: 253.63, volume: 27890.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:55:00.000Z"), close: 254.03, volume: 40270.00} ])
If you are running MongoDB on Atlas, you can click Browse collections to view the sample data.
Query the data.
You query a time series collection like any other MongoDB collection. For more information, see About Querying Time Series Data.
Common queries for time series data are querying the metaField
to get data for a single time series, or using a range query on the
timeField
to get data for a given time span.
To query the metaField
for a single time series:
db.stocks.find( { ticker: "MDB" } )
To query the timeField
for a time span:
db.stocks.find({ date : { $gte : ISODate("2021-12-18T15:50:00.000Z"), $lte : ISODate("2021-12-18T15:56:00.000Z")} });
Install the dependencies.
For detailed instructions, see Prerequisites.
Install the Atlas CLI.
If you use Homebrew, you can run the following command in your terminal:
brew install mongodb-atlas-cli For installation instructions on other operating systems, see Install the Atlas CLI
Install Docker.
Docker requires a network connection for pulling and caching MongoDB images.
For MacOS or Windows, install Docker Desktop v4.31+.
For Linux, install Docker Engine v27.0+.
For RHEL, you can also use Podman v5.0+.
Set up your local Atlas deployment.
If you don't have an existing Atlas account, run
atlas setup
in your terminal or create a new account.Run
atlas deployments setup
and follow the prompts to create a local deployment. When prompted to connect to the deployment, selectskip
.For detailed instructions, see Create a Local Atlas Deployment.
Install mongosh
.
Open a new terminal window and connect to your deployment.
Use mongosh
to connect to your self-managed or Atlas deployment.
For example:
mongosh "mongodb+srv://my-test-cluster.1twap.mongodb.net/" --apiVersion 1 --username <user>
Create an empty time series collection.
Note
This exercise uses stock ticker sample data. The date
field stores time data, and
the ticker
field identifies the individual stock.
Set the
timeField
,metaField
, andgranularity
:timeseries: { timeField: "date", metaField: "ticker", granularity: "seconds" } OR specify custom granularity:
New in version 6.3.
timeseries: { timeField: "date", metaField: "ticker", granularity: "seconds", bucketMaxSpanSeconds: "300", bucketRoundingSeconds: "300" } Create the collection using the
db.createCollection()
method:db.createCollection( "stocks", { timeseries: { timeField: "date", metaField: "ticker", granularity: "seconds" } }) This creates an empty time series collection named
stocks
.
Add sample documents.
Run the db.collection.insertMany()
method to add the
following sample documents to the collection:
db.stocks.insertMany([ { ticker: "MDB", date: ISODate("2021-12-18T15:59:00.000Z"), close: 252.47, volume: 55046.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:58:00.000Z"), close: 252.93, volume: 44042.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:57:00.000Z"), close: 253.61, volume: 40182.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:56:00.000Z"), close: 253.63, volume: 27890.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:55:00.000Z"), close: 254.03, volume: 40270.00} ])
If you are running MongoDB on Atlas, you can click Browse collections to view the sample data.
Query the data.
You query a time series collection like any other MongoDB collection. For more information, see About Querying Time Series Data.
Common queries for time series data are querying the metaField
to get data for a single time series, or using a range query on the
timeField
to get data for a given time span.
To query the metaField
for a single time series:
db.stocks.find( { ticker: "MDB" } )
To query the timeField
for a time span:
db.stocks.find({ date : { $gte : ISODate("2021-12-18T15:50:00.000Z"), $lte : ISODate("2021-12-18T15:56:00.000Z")} });
Install MongoDB and dependencies.
If you're running a self-managed deployment, follow the installation instructions for your MongoDB version, edition, and platform.
Install mongosh
.
Open a new terminal window and connect to your deployment.
Use mongosh
to connect to your self-managed or Atlas deployment.
For example:
mongosh "mongodb+srv://my-test-cluster.1twap.mongodb.net/" --apiVersion 1 --username <user>
Create an empty time series collection.
Note
This exercise uses stock ticker sample data. The date
field stores time data, and
the ticker
field identifies the individual stock.
Set the
timeField
,metaField
, andgranularity
:timeseries: { timeField: "date", metaField: "ticker", granularity: "seconds" } OR specify custom granularity:
New in version 6.3.
timeseries: { timeField: "date", metaField: "ticker", granularity: "seconds", bucketMaxSpanSeconds: "300", bucketRoundingSeconds: "300" } Create the collection using the
db.createCollection()
method:db.createCollection( "stocks", { timeseries: { timeField: "date", metaField: "ticker", granularity: "seconds" } }) This creates an empty time series collection named
stocks
.
Add sample documents.
Run the db.collection.insertMany()
method to add the
following sample documents to the collection:
db.stocks.insertMany([ { ticker: "MDB", date: ISODate("2021-12-18T15:59:00.000Z"), close: 252.47, volume: 55046.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:58:00.000Z"), close: 252.93, volume: 44042.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:57:00.000Z"), close: 253.61, volume: 40182.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:56:00.000Z"), close: 253.63, volume: 27890.00}, { ticker: "MDB", date: ISODate("2021-12-18T15:55:00.000Z"), close: 254.03, volume: 40270.00} ])
If you are running MongoDB on Atlas, you can click Browse collections to view the sample data.
Query the data.
You query a time series collection like any other MongoDB collection. For more information, see About Querying Time Series Data.
Common queries for time series data are querying the metaField
to get data for a single time series, or using a range query on the
timeField
to get data for a given time span.
To query the metaField
for a single time series:
db.stocks.find( { ticker: "MDB" } )
To query the timeField
for a time span:
db.stocks.find({ date : { $gte : ISODate("2021-12-18T15:50:00.000Z"), $lte : ISODate("2021-12-18T15:56:00.000Z")} });
Sample Data
This quick start creates a stocks
time series collection with the
following document structure.
{ _id: ObjectId(...), ticker: <string>, date: ISODate(...), close: <double>, volume: <double> }
Learning Summary
This quick start focused on creating a new time series collection. Because time series collections are optimized for time data, their performance depends heavily on how you configure them at creation. For more information, see Time Series Collection Considerations.
Next Steps
To migrate existing data into a time series collection, see Migrate Data into a Time Series Collection.
To shard a time series collection, see Shard a Time Series Collection.
For aggregation and query behaviors specific to time series collections, see Aggregation and Operator Considerations.
Learn More
To learn more about how MongoDB stores time series data internally, see About Time Series Data.
To learn more about custom bucketing parameters in MongoDB 6.3 and later, see Using Custom Bucketing Parameters.