Week 5 – Homework
Load the sample data provided by MongoDB into your Atlas cluster.
Task 1
For task one, we are going to work with sample_weatherdata.data. You should
select only documents that match the following criteria:
callLettersis “SHIP”pressure.valueis greater than 2000atmosphericPressureChange.tendency.codefalls between 4 and 7precipitationEstimatedObservation.discrepancyis equal to 2
In order to perform this task, you need to create a variable as follows:
var match = {$match: {...}}
How many documents match these conditions?
Answer: 3
Task 2
Define a project stage in order to:
- not display
_id - display
ts,position.coordinates,callLetters - rename
pressure.valueas pressure - rename
atmosphericPressureChange.tendency.codeas change - rename
precipitationEstimatedObservation.discrepancyas discrepancy
Create a new variable as follows:
var project = {$project: {...}}
Pass both the match stage defined in task one and the newly created
project within your aggregation pipeline.
This is the expected output:
{
"ts" : ISODate("1984-03-08T12:00:00Z"),
"position" : {
"coordinates" : [
171.2,
23.2
]
},
"callLetters" : "SHIP",
"pressure" : 9999.9,
"change" : "4",
"discrepancy" : "2"
}
{
"ts" : ISODate("1984-03-12T18:00:00Z"),
"position" : {
"coordinates" : [
4.5,
49.5
]
},
"callLetters" : "SHIP",
"pressure" : 9999.9,
"change" : "6",
"discrepancy" : "2"
}
{
"ts" : ISODate("1984-03-12T12:00:00Z"),
"position" : {
"coordinates" : [
-9.1,
43.6
]
},
"callLetters" : "SHIP",
"pressure" : 9999.9,
"change" : "6",
"discrepancy" : "2"
}
Task 3
Let’s turn to tate.artists. You are required to design an aggregation, doing the following:
- select all documents for which the value of
genderis equal to “Female” - group observations on the basis of movement names (to do that you should
deconstruct the
movNamesarray with $unwind) - retrieve the 5 most frequent movements
This is the expected result:
{ "_id" : "Feminist Art", "count" : 18 }
{ "_id" : "Performance Art", "count" : 10 }
{ "_id" : "Young British Artists (YBA)", "count" : 9 }
{ "_id" : "Conceptual Art", "count" : 9 }
{ "_id" : "Abject art", "count" : 8 }