Docs Menu
Docs Home
/ / /
Scala
/

Time Series Data

On this page

  • Overview
  • Create a Time Series Collection
  • Store Time Series Data
  • Query Time Series Data
  • Additional Information

In this guide, you can learn how to use the Scala driver to store and interact with time series data.

Time series data is composed of the following components:

  • Measured quantity

  • Timestamp for the measurement

  • Metadata that describes the measurement

The following table describes sample situations for which you could store time series data:

Situation
Measured Quantity
Metadata

Recording monthly sales by industry

Revenue in USD

Company, country

Tracking weather changes

Precipitation level

Location, sensor type

Recording fluctuations in housing prices

Monthly rent price

Location, currency

Important

Server Version for Time Series Collections

To create and interact with time series collections, you must be connected to a deployment running MongoDB Server 5.0 or later.

You can create a time series collection to store time series data. To create a time series collection, pass the following parameters to the createCollection() method:

  • Name of the new collection to create

  • CreateCollectionOptions object with the TimeSeriesOptions set using the timeSeriesOptions() method

This example creates the october2024 time series collection in the fall_weather database with the timeField option set to the "timestamp" field and the metaField option set to the "location" field:

val database = mongoClient.getDatabase("fall_weather")
val tsOptions = TimeSeriesOptions("timestamp").metaField("location")
val collectionOptions = CreateCollectionOptions().timeSeriesOptions(tsOptions)
val createObservable = database.createCollection("october2024", collectionOptions)
Await.result(createObservable.toFuture(), Duration(10, TimeUnit.SECONDS))

To verify that you successfully created the time series collection, run the listCollections() method on the database and print the results:

val listObservable = database.listCollections()
val list = Await.result(listObservable.toFuture(), Duration(10, TimeUnit.SECONDS))
val jsonSettings = JsonWriterSettings.builder().indent(true).build()
list.foreach(collection => {
println(collection.toJson(jsonSettings))
})
{
"name": "october2024",
"type": "timeseries",
"options": {
"timeseries": {
"timeField": "temperature",
"granularity": "seconds",
"bucketMaxSpanSeconds": 3600
}
},
"info": {
"readOnly": false
}
}
...

You can insert data into a time series collection by using the insertOne() or insertMany() methods and specifying the measurement, timestamp, and metadata in each inserted document.

Tip

To learn more about inserting documents into a collection, see the Insert Documents guide.

This example inserts New York City temperature data into the october2024 time series collection created in the Create a Time Series Collection example. Each document contains the following fields:

  • temperature, which stores temperature measurements in degrees Fahrenheit

  • location, which stores location metadata

  • timestamp, which stores the time of the measurement collection

val collection = database.getCollection("october2024")
val temperatures = Seq(
Document("timestamp" -> BsonDateTime(1727755200000L), "temperature" -> 54, "location" -> "New York City"),
Document("timestamp" -> BsonDateTime(1727841600000L), "temperature" -> 55, "location" -> "New York City"),
)
val insertObservable = collection.insertMany(temperatures)
Await.result(insertObservable.toFuture(), Duration(10, TimeUnit.SECONDS))

You can use the same syntax and conventions to query data stored in a time series collection as you use when performing read or aggregation operations on other collections. To learn more about these operations, see the Additional Information section.

To learn more about the concepts mentioned in this guide, see the following MongoDB Server manual entries:

To learn more about performing read operations, see Read Data from MongoDB.

To learn more about performing aggregation operations, see the Transform Your Data with Aggregation guide.

To learn more about the methods mentioned in this guide, see the following API documentation:

Back

Run a Command