Create and Use a Time-Series Collection in MongoDB
📖 Scenario: You are managing a small weather station that records temperature and humidity every hour. You want to store this data efficiently using MongoDB's time-series collections.
🎯 Goal: Build a time-series collection in MongoDB to store hourly weather data with timestamps, then insert and query the data.
📋 What You'll Learn
Create a time-series collection named weatherData with time as the time field
Set the meta field to location to store the station location
Insert three sample documents with exact timestamps and values
Query the collection to find all records for the location StationA
💡 Why This Matters
🌍 Real World
Time-series collections are used in IoT, monitoring, and logging systems to efficiently store and query data that changes over time.
💼 Career
Understanding time-series collections is important for database administrators and backend developers working with real-time data and analytics.
Progress0 / 4 steps
1
Create the time-series collection
Use the db.createCollection command to create a time-series collection called weatherData with time as the time field and location as the meta field.
MongoDB
Hint
Use db.createCollection with the timeseries option specifying timeField and metaField.
2
Insert sample weather data
Insert three documents into weatherData with these exact fields: time (ISODate), location (string), temperature (number), and humidity (number). Use these exact values: 2024-06-01T10:00:00Z, StationA, 22.5, 60; 2024-06-01T11:00:00Z, StationA, 23.0, 58; 2024-06-01T12:00:00Z, StationA, 24.0, 55.
MongoDB
Hint
Use insertMany with an array of documents. Use ISODate for the time field.
3
Query data for StationA
Write a query using find on weatherData to get all documents where location is exactly "StationA".
MongoDB
Hint
Use find with a filter object specifying location: "StationA".
4
Create an index on the time field
Create an index on the time field in the weatherData collection to optimize queries by time.
MongoDB
Hint
Use createIndex on the time field with ascending order 1.
Practice
(1/5)
1. What is the main purpose of a time-series collection in MongoDB?
easy
A. To manage user authentication and roles
B. To store large binary files like images and videos
C. To efficiently store and query data that changes over time
D. To create backups of the database automatically
Solution
Step 1: Understand time-series data
Time-series data is data recorded over time, like temperature or stock prices.
Step 2: Identify MongoDB time-series collection use
MongoDB time-series collections optimize storage and queries for such time-based data.
Final Answer:
To efficiently store and query data that changes over time -> Option C
Quick Check:
Time-series collections = efficient time-based data storage [OK]
Hint: Time-series collections are for data changing over time [OK]
Common Mistakes:
Confusing time-series collections with file storage
Thinking time-series collections manage users
Assuming they handle backups automatically
2. Which of the following is required when creating a time-series collection in MongoDB?
easy
A. A field that stores binary data
B. A unique index on the _id field
C. A field that stores user roles
D. A field that stores the timestamp of each record
A. Missing required timeField in timeseries options
B. metaField cannot be used in time-series collections
C. Collection name is invalid
D. timeseries option is misspelled
Solution
Step 1: Check timeseries options in the command
The command specifies metaField but omits the required timeField.
Step 2: Recall MongoDB requirements
timeField is mandatory for time-series collections; missing it causes an error.
Final Answer:
Missing required timeField in timeseries options -> Option A
Quick Check:
timeField missing = error [OK]
Hint: Always include timeField when creating time-series collections [OK]
Common Mistakes:
Thinking metaField is not allowed
Assuming collection name is invalid
Believing timeseries option is misspelled
5. You have a time-series collection with fields timestamp (timeField) and sensorId (metaField). You want to query the average temperature for sensorId 'S1' over the last 24 hours. Which aggregation pipeline is correct?