Skip to content

Commit cdcf4ac

Browse files
committed
Finished week 4
1 parent 7d5ebd7 commit cdcf4ac

File tree

15 files changed

+692
-0
lines changed

15 files changed

+692
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { MongoClient } = require("mongodb");
2+
3+
const uri = "mongodb+srv://daryna2003tk_db_user:[email protected]/dbWeek4?retryWrites=true&w=majority";
4+
const client = new MongoClient(uri);
5+
6+
async function totalPopulationByCountry(countryName) {
7+
try {
8+
await client.connect();
9+
const db = client.db("dbWeek4");
10+
const collection = db.collection("population");
11+
12+
const cursor = collection.find({ Country: countryName });
13+
const populationByYear = {};
14+
15+
await cursor.forEach(doc => {
16+
const year = doc.Year;
17+
const total = doc.M + doc.F;
18+
if (!populationByYear[year]) populationByYear[year] = 0;
19+
populationByYear[year] += total;
20+
});
21+
22+
const result = Object.keys(populationByYear).sort().map(year => ({
23+
_id: parseInt(year),
24+
countPopulation: populationByYear[year]
25+
}));
26+
27+
console.log(result);
28+
29+
} catch (err) {
30+
console.error(err);
31+
} finally {
32+
client.close();
33+
}
34+
}
35+
36+
totalPopulationByCountry("Netherlands");
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { MongoClient } = require("mongodb");
2+
3+
const uri = "mongodb+srv://daryna2003tk_db_user:[email protected]/dbWeek4?retryWrites=true&w=majority";
4+
const client = new MongoClient(uri);
5+
6+
async function continentPopulation(yearValue, ageValue) {
7+
try {
8+
await client.connect();
9+
const db = client.db("dbWeek4");
10+
const collection = db.collection("population");
11+
12+
const results = await collection.aggregate([
13+
{ $match: { Year: yearValue, Age: ageValue, Country: { $in: ["AFRICA", "ASIA", "EUROPE", "LATIN AMERICA AND THE CARIBBEAN", "NORTHERN AMERICA", "OCEANIA"] } } },
14+
{ $project: { Country: 1, Year: 1, Age: 1, M: 1, F: 1, TotalPopulation: { $add: ["$M", "$F"] } } }
15+
]).toArray();
16+
17+
console.log(results);
18+
} catch (err) {
19+
console.error(err);
20+
} finally {
21+
client.close();
22+
}
23+
}
24+
25+
continentPopulation(2020, "100+");

Week4/homework/ex1-aggregation/package-lock.json

Lines changed: 166 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "ex1-aggregation",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "exercise1_population.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"mongodb": "^7.0.0"
14+
}
15+
}
256 KB
Loading
120 KB
Loading
223 KB
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { setup } = require("./setup");
2+
const { transfer } = require("./transfer");
3+
4+
async function start() {
5+
await setup();
6+
7+
await transfer(101, 102, 1000, "Test transfer");
8+
}
9+
10+
start();

0 commit comments

Comments
 (0)