- Practice the concepts
- Prep exercises
- MongoDB exercises
- Code along
Let's practice some advanced MongoDB queries. Have a look at the following exercises. If you feel unsure about the commands you used last week then feel free to also do 01 and 02 of this series. You will need to go back to 00 to import the data into your database, the datasets can be found here:
Prep exercises are exercises that you should work on before the session on Sunday. These are a little more difficult or show an important concept and as such are a great exercise to talk about with your mentor. Have a solution ready by Sunday as you may be asked to show what you did.
The QA_PREP_EXERCISE.md file explains what needs to be done for the prep exercise this week. There will also be some questions at the bottom to think about. Go through them before the session on Sunday as it will be covered then.
Let's create a fresh database for this week's assignment. You can create a database called databaseWeek4
that you can
use for the following exercises.
Let's practice some aggregation queries, for that we will have to use some data and
luckily kaggle is a great website that provides datasets to use. In the ex1-aggregation
folder you will find a csv file with data for you.
- Find a way to get the data in the csv file into your MongoDB database. The documents should look like:
{
_id: ObjectId(625ff77ada84ee8b5dd06e82),
Country: "Afghanistan",
Year: 1950,
Age: "20-24",
M: 374109,
F: 318392
}
- Write a function that will return the array of the total population (M + F over all age groups) for a given
Country
per year. The result should look something like this, these are the values forNetherlands
:
[
{
_id: 1950,
countPopulation: 10042051
},
{
_id: 1960,
countPopulation: 11448815
},
{
_id: 1970,
countPopulation: 13001941
},
{
_id: 1980,
countPopulation: 14148410
},
{
_id: 1990,
countPopulation: 14965442
},
{
_id: 2000,
countPopulation: 15926188
},
{
_id: 2010,
countPopulation: 16682925
},
{
_id: 2020,
countPopulation: 17134872
},
{
_id: 2022,
countPopulation: 17211448
}
];
- Write a function that will return all the information of each continent for a given
Year
andAge
field but add a new fieldTotalPopulation
that will be the addition ofM
andF
. For example, if I would give2020
for theYear
and100+
for theAge
it should return something like this:
[
{
_id: new
ObjectId(
"62600561b0a05834e3382cf8"
),
Country: "AFRICA",
Year: 2020,
Age: "100+",
M: 1327,
F: 2723,
TotalPopulation: 4050
},
{
_id: new
ObjectId(
"62600561b0a05834e3382da0"
),
Country: "ASIA",
Year: 2020,
Age: "100+",
M: 57019,
F: 207883,
TotalPopulation: 264902
},
{
_id: new
ObjectId(
"62600561b0a05834e33832a1"
),
Country: "EUROPE",
Year: 2020,
Age: "100+",
M: 22579,
F: 102056,
TotalPopulation: 124635
},
{
_id: new
ObjectId(
"62600561b0a05834e33835d4"
),
Country: "LATIN AMERICA AND THE CARIBBEAN",
Year: 2020,
Age: "100+",
M: 19858,
F: 49218,
TotalPopulation: 69076
},
{
_id: new
ObjectId(
"62600561b0a05834e3383946"
),
Country: "NORTHERN AMERICA",
Year: 2020,
Age: "100+",
M: 22267,
F: 83419,
TotalPopulation: 105686
},
{
_id: new
ObjectId(
"62600561b0a05834e3383985"
),
Country: "OCEANIA",
Year: 2020,
Age: "100+",
M: 1094,
F: 3980,
TotalPopulation: 5074
}
];
Just like last week, let's solve the same transaction problem but then in MongoDB. You can use the same data as in
Week3. Note that you will need to include some libraries which means you will need to set that up as well (create
a ex2-transactions
folder for this). You will also probably want to create an index.js
that calls the functions we
will create to test it out, we leave the implementation of that up to you. Now let's get started, we will split our code
into multiple files again, first being the setup file:
- Create a
setup.js
file. - It should clean up the
accounts
array and then fill it with some sample data. Just like last last week we want an account document to have anaccount_number
andbalance
field. Then it should have another field calledaccount_changes
that is an array that contains the fields:change_number, amount, changed_date, remark
. - It's probably best to make this a function that you can export and call
Then it is time to write our transaction function:
- Create a
transfer.js
file that will hold ourtransfer
function. - It should transfer money from one account to another, so it will need to know the following things: from which account, to which account, the amount and the remark for this transaction.
- This should update the balances of both accounts and for each account add a change to the list. The change number
should be incremented, so if the latest
change_number
is 30, thechange_number
for the new change should be 31. - Test that it works by calling the function to transfer 1000 from account number 101 to account number 102.
Submit the setup.js
and transfer.js
files.
This week we have a small code along to show how to implement pagination and make it reusable for all your routes!
- Paginated API with Node and Mongoose. We will be working in Mongoose in the final project, but this gives a little teaser on what mongoose is!
After you've finished your todo list it's time to show us what you got! The assignment that needs to be submitted is the following:
- MongoDB exercises
Upload your code to your forked Databases repository in GitHub. Make a pull request to HackYourAssignments's forked repository.
Forgotten how to upload your assignment? Go through the guide to learn how to do this again.
Deadline Tuesday 23.59 CET