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:
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:
// 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
{
$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.
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:
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