Create Time Series Collections with VS Code
You can create time series collections using a MongoDB Playground.
Prerequisites
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:
Create a Time Series Collection
Once you connect to your deployment using MongoDB for VS code, use the left navigation to:
Expand an active connection and hover over the database where you want your collection to exist.
Click the icon that appears.
A MongoDB playground automatically opens with a template form to create both regular collections and time series collections.
Delete the regular collection form and uncomment the time series form.
Fill in the provided fields with details for your time series collection.
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.
Tip
See also:
To learn more about time series collections, see Time Series Collections
To add documents to the collection, see Create Documents with VS Code.
Example
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 istimestamp
.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.