Docs Menu

Create Time Series Collections

On this page

  • Prerequisites
  • Create a Time Series Collection
  • Example
Note
Preview

MongoDB for VS Code is currently available as a Preview in the Visual Studio Marketplace. The product, its features, and the corresponding documentation may change during the Preview stage.

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

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.

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 CollectionsPerform CRUD Operations →
Give Feedback
© 2022 MongoDB, Inc.

About

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