0
0
MongoDBquery~30 mins

Time-series collections in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use createIndex on the time field with ascending order 1.