Working with MongoDB Time Series Collections

Creating Time Series Collections

To create a time series collection in MongoDB, developers can use the db.createCollection() command with specific time series parameters:

JavaScript
db.createCollection(
    "weather",
    {
       timeseries: {
          timeField: "timestamp",
          metaField: "metadata",
          granularity: "hours"
       }
    }
)

Populating and Querying Data

Data insertion and retrieval follow MongoDB conventions but leverage the optimized storage format of time series collections:

JavaScript
// Inserting data into 'weather' collection
db.weather.insertMany( [
   {
      "metadata": { "sensorId": 5578, "type": "temperature" },
      "timestamp": ISODate("2021-05-18T00:00:00.000Z"),
      "temp": 12
   },
   // Additional data points...
] )

// Querying specific data
db.weather.findOne({
   "timestamp": ISODate("2021-05-18T00:00:00.000Z")
})

// Performing aggregation pipelines
db.weather.aggregate( [
   {
      $group: {
         _id: { $dateToString: { format: "%Y-%m-%d", date: "$timestamp" } },
         avgTemp: { $avg: "$temp" }
      }
   }
] )

Managing Time Series Data

MongoDB provides tools for managing time series data, such as automatic document expiration and gap filling for missing data points.

Interpolating Missing Data

MongoDB 5.3 introduces gap filling capabilities using $densify and $fill commands to handle missing data points effectively.

Example of Gap Filling

JavaScript
{
  $densify: {
      field: "timestamp",
      partitionByFields: ["metadata.sensorId"],
      range: {
        step: 1,
        unit: "hour",
        bounds: "partition"
      }
  }
}

Example: Below is an example to store time-series data in MongoDB.

JavaScript
const { MongoClient } = require('mongodb');

// Connection URI
const uri = 'mongodb://localhost:27017';

// Database Name
const dbName = 'mydatabase';

// Create a new MongoClient
const client = new MongoClient(uri, { useUnifiedTopology: true });

async function main() {
    try {
        // Connect to the MongoDB server
        await client.connect();
        console.log('Connected to MongoDB');

        // Reference the database
        const db = client.db(dbName);

        // Function to insert data into the collection
        const insertData = async (collectionName, timestamp, value) => {
            const collection = db.collection(collectionName);
            const result = await collection.insertOne({ timestamp, value });
            console.log(`Inserted data into ${collectionName}`);
            return result;
        };

        // Insert some sample data into collections
        await insertData('temperature', new Date('2024-05-16T08:00:00'), 25);
        await insertData('temperature', new Date('2024-05-16T08:15:00'), 26);
        await insertData('temperature', new Date('2024-05-16T08:30:00'), 27);
        await insertData('humidity', new Date('2024-05-16T08:00:00'), 50);
        await insertData('humidity', new Date('2024-05-16T08:15:00'), 55);
        await insertData('humidity', new Date('2024-05-16T08:30:00'), 60);

        // Query and print data from the collections
        const queryData = async (collectionName) => {
            const collection = db.collection(collectionName);
            const cursor = collection.find().sort({ timestamp: 1 });
            console.log(`Data in collection '${collectionName}':`);
            await cursor.forEach(console.log);
        };

        await queryData('temperature');
        await queryData('humidity');
    } catch (error) {
        console.error('Error:', error);
    } finally {
        // Close the connection
        await client.close();
        console.log('Disconnected from MongoDB');
    }
}

// Run the main function
main();

Output:


Store time-series data in MongoDB

How to store time-series data in MongoDB?

In the realm of data management, time series data presents unique challenges due to its sequential and timestamped nature. MongoDB, a leading NoSQL database, has introduced native support for time series data starting from version 5.0, offering enhanced capabilities in storing and querying this specialized data type.

Table of Content

  • Time Series Data
  • Components of Time Series Data
  • Challenges in Managing Time Series Data
  • MongoDB Time Series Collections
  • Key Features and Benefits
  • Working with MongoDB Time Series Collections
  • FAQs

Similar Reads

Time Series Data

A time series is essentially a sequence of data points indexed (or listed) in time order. Each data point is associated with a timestamp, providing a chronological dimension to the dataset. This form of data is pervasive across various domains, from IoT sensor readings to financial market fluctuations....

Components of Time Series Data

Time series data typically consists of:...

Challenges in Managing Time Series Data

The nature of time series data presents challenges in storage and retrieval:...

MongoDB Time Series Collections

MongoDB’s Time Series Collections provide a tailored solution for storing time-based data efficiently. In time series collections, data points from the same source are efficiently stored alongside other data points sharing a similar timestamp. This organization optimizes write operations by clustering related data, enhancing retrieval speed and facilitating analysis of sequential data patterns....

Key Features and Benefits

Columnar Storage: Data is stored in a columnar format optimized for time-ordered retrieval, reducing disk usage and improving query performance.Automatic Indexing: MongoDB automatically creates clustered indexes on the time field, enhancing query efficiency.Usability: Time Series Collections offer familiar MongoDB functionalities, enabling standard CRUD operations and aggregation pipelines....

Working with MongoDB Time Series Collections

Creating Time Series Collections...

FAQs

Can existing MongoDB collections be converted into time series collections?...

Contact Us