Example: Create a time series through the wire listener

This example shows how to create, load, and query a time series with the MongoDB API through the wire listener.

Before you begin

Before you start this example, ensure these tasks are complete:

  • Connect to a database in which to create the time series table. You run all methods in the database.
  • Configure the wire listener for the MongoDB API. For more information, see Configuring the wire listener for the first time.
  • Define a dbspace that is named dbspace1. For more information, see Dbspaces.

About this task

In this example, you create a time series that contains sensor readings about the temperature and humidity in a house. Readings are taken every 10 minutes. The following table lists the time series properties that are used in this example.
Table 1. Time series properties used in this example
Time series property Definition
Timepoint size 10 minutes
When timepoints are valid Every 10 minutes
Data in the time series The following data:
  • Timestamp
  • A float value that represents temperature
  • A float value that represents humidity
Time series table The following columns:
  • A meter ID column of type INTEGER
  • A TimeSeries data type column
Origin 2014-01-01 00:00:00.00000
Regularity Regular
Where to store the data In a container that you create
How to load the data Through a virtual table
How to access the data Through a virtual table

Procedure

To create a time series with the MongoDB API mongo shell:
  1. Create a time series calendar. The time series calendar is named ts_10min, with a calendar and pattern start date of 2014-01-01 00:00:00, a calendar pattern that is defined with intervals of minutes, and data is recorded in 10 minute increments after the origin.
    MongoDB API
    Add to the predefined system.timeseries.calendar collection.
    db.system.timeseries.calendar.insert({"name":"ts_10min", 
     "calendarStart":"2014-01-01 00:00:00",
     "patternStart":"2014-01-01 00:00:00", 
     "pattern":{"type":"minute", 
              "intervals":[{"duration":"1","on":"true"}, 
                         {"duration":"9","on":"false"}]}})
  2. Create a TimeSeries row type. The row type is named reading and includes fields for timestamp, temperature, and humidity.
    MongoDB API
    Add to the predefined system.timeseries.rowType collection.
    db.system.timeseries.rowType.insert({"name":"reading",
    "fields":[{"name":"tstamp","type":"datetime year to fraction(5)"},
            {"name":"temp","type":"float"},
            {"name":"hum","type":"float"}]})
  3. Create a container. The container is named c_0 and is created in the dbspace1 dbspace, in the reading time series row, with a first extent size of 1000, and with growth increments of 500.
    MongoDB API
    Add to the predefined system.timeseries.container collection.
    db.system.timeseries.container.insert({"name":"c_0",
     "dbspaceName":"dbspace1",
     "rowTypeName":"reading",
     "firstExtent":1000,
     "nextExtent":500})
  4. Create a time series table. The time series table is named ts_data1 and includes id and ts columns.
    MongoDB API
    Create the ts_data1 time series table:
    db.runCommand({"create":"ts_data1",
     "columns":[{"name":"id","type":"int","primaryKey":"true","notNull":"true"},
              {"name":"ts","type":"timeseries(reading)"}]})
  5. Create a virtual table. The virtual table is named ts_data1_v and is based on the time series table that is named ts_data1 and its timeseries column ts, using the ts_10min calendar, starting on 2014-01-01 00:00:00.00000, in the time series container c_0, with the virtualTableMode parameter set to 0 (default).
    Important: This example contains line breaks for page formatting, however, JSON does not allow line breaks within strings.
    MongoDB API
    Create the ts_data1_v virtual table:
    db.runCommand({"create":"ts_data1_v",
     "timeseriesVirtualTable":
             {"baseTableName":"ts_data1",
              "newTimeseries":"calendar(ts_10min),origin(2014-01-01 00:00:00.00000),container(c_0)", 
              "virtualTableMode":0,
              "timeseriesColumnName":"ts"}})
  6. Load records into the time series by inserting documents into the ts_data1_v virtual table.

    Because this time series is regular, you are not required to include the time stamp. The first record is inserted for the origin of the time series, 2014-01-01 00:00:00.00000. The second record has the time stamp 2014-01-01 00:10:00.00000, and the third record has the time stamp 2014-01-01 00:20:00.00000.

    MongoDB API
    Add documents to the ts_data1_v virtual table:
    db.ts_data1_v.insert([{"id":1,"temp":15.0,"hum":20.0},	{"id":1,"temp":16.2,hum:19.0},{id:1,temp:16.5,hum:22.0}])
  7. Query the time series data by using the ts_data1_v virtual table.
    MongoDB API
    Query the ts_data1_v virtual table:
    db.ts_data1_v.find()
    
    Results:
    > db.ts_data1_v.find()
    {"id":1,"tstamp":ISODate("2014-01-01T06:00:00Z"),"temp":15,"hum":20}
    {"id":1,"tstamp":ISODate("2014-01-01T06:10:00Z"),"temp":16.2,"hum":19}
    {"id":1,"tstamp":ISODate("2014-01-01T06:20:00Z"),"temp":16.5,"hum":22}