Docs Menu

Docs HomeDevelop ApplicationsMongoDB for VS Code

Create Time Series Collections

On this page

  • Prerequisites
  • Create a Time Series Collection
  • Example

You can create time series collections using a MongoDB Playground.

If you have not done so already, you must complete the following prerequisites before you can create a time series collection with a MongoDB Playground:

Once you connect to your deployment using MongoDB for VS code, use the left navigation to:

  1. Expand an active connection and hover over the database where you want your collection to exist.

  2. Click the icon that appears.

  3. A MongoDB playground automatically opens with a template form to create both regular collections and time series collections.

  4. Delete the regular collection form and uncomment the time series form.

  5. Fill in the provided fields with details for your time series collection.

  6. To run the playground, click the Play Button at the top right of the VS code navigation bar.

After running the playground with the time series collection, the left navigation will update the collection icon to identify it is a time series collection.

Image showing time series icon in VS Code extension
click to enlarge

Tip

See also:

This example creates a time series collection named weather in the test database.

To use this example, start with a collection template from your MongoDB Playgrounds. Delete the template form for regular collections and keep the template for time series collections found below the regular collection template.

use('test');
db.createCollection(
"weather",
{
timeseries: {
timeField: "timestamp",
granularity: "hours",
bucketMaxSpanSeconds: 60,
bucketRoundingSeconds: 60
}
}
)

In the example:

  • use('test') selects the database where the collection is added to.

  • timeseries specifies fields to create a time series collection.

    • timeField: "timestamp" names the field that contains the dates in the time series documents. In this case, it is timestamp.

    • granularity: "hours" defines the time scale by which the documents are stored.

    • bucketMaxSpanSeconds defines a maximum time span of 60 seconds for each bucket.

    • bucketRoundingSeconds specifies the time interval that determines the starting timestamp for a new bucket.

When you press the Play Button, MongoDB for VS Code splits your Playground and outputs the following result in the Playground Results.json pane to confirm the creation of the time series collection.

{
"ok": 1
}

The weather collection also appears in your collection list, and is marked with the time series icon.

← Create Databases and Collections