*** Time Series Collections ***
1. Make sure you have MongoDB Database Tools installed
(https://docs.mongodb.com/database-tools/installation/installation/).
2. Make sure you have a local MongoDB instance running (see lab #2) or use the
remote MongoDB Atlas instance (see lab #1). You'll need at least MongoDB v5 for
this lab.
3. Download the Timisoara 2020 weather data
4. Connect to MongoDB using shell
mongo mongodb://mongoadmin:secret@localhost:27888/?authSource=admin
5. Create the database
> use weather
6. Create the time series collection to hold the historic weather data
> db.createCollection(
"timisoara",
{
timeseries: {
timeField: "timestamp",
granularity: "hours"
}
}
)
6. Import the weather sample data (make sure you run this at the command prompt,
not inside the Mongo Shell)
mongoimport --db=weather --collection=timisoara
mongodb://mongoadmin:secret@localhost:27888/?authSource=admin
timisoara_2020_weather.json
7. See some sample documents, to get a taste of the sample data
> use weather
> db.timisoara.find({}).limit(3).pretty()
8. Get average temperature for each day in the data sample
> db.timisoara.aggregate( [
{
$project: {
date: {
$dateToParts: { date: "$timestamp" }
},
temp: 1
}
},
{
$group: {
_id: {
date: {
year: "$date.year",
month: "$date.month",
day: "$date.day"
}
},
avgTmp: { $avg: "$temp" }
}
}
] )
DO IT YOURSELF:
A. Get average temperature for each month in the data sample.
B. Display the previous list in descending order of the average temperatures.
NEXT:
It's about time to check whether you master MongoDB.
Next time we'll see the project that will get you the grade for this semester. For
now just relax.