Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Week4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
node_modules/
Binary file added Week4/homework/ex1-aggregation/1-Visualize.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions Week4/homework/ex1-aggregation/1-importDataPopulation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fs from "fs";
import csv from "csv-parser";
import { MongoClient } from "mongodb";
import dotenv from "dotenv";
dotenv.config();

const client = new MongoClient(process.env.MONGO_URI);

async function run() {
try {
await client.connect();
console.log("Connected to MongoDB");

const db = client.db("databaseWeek4");
const collection = db.collection("population");

const results = [];

fs.createReadStream("./homework/ex1-aggregation/population_pyramid_1950-2022.csv")
.pipe(csv())
.on("data", (row) => {
results.push({
country: row.Country,
year: Number(row.Year),
age: row.Age,
male: Number(row.M),
female: Number(row.F),
});
})
.on("end", async () => {
const inserted = await collection.insertMany(results);
console.log("Inserted:", inserted.insertedCount, "documents");
await client.close();
});

} catch (err) {
console.error(err);
}
}

run();
Binary file added Week4/homework/ex1-aggregation/2-Nederlands.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions Week4/homework/ex1-aggregation/2-TotalPopulation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { MongoClient } from "mongodb";
import dotenv from "dotenv";
dotenv.config();

const client = new MongoClient(process.env.MONGO_URI);

async function getPopulationByCountry(countryName) {
try {
await client.connect();
const db = client.db("databaseWeek4");
const collection = db.collection("population");

const result = await collection.aggregate([
{ $match: { Country: countryName } },
{
$group: {
_id: "$Year",
countPopulation: { $sum: { $add: ["$M", "$F"] } }
}
},
{ $sort: { _id: 1 } }
]).toArray();

return result;

} catch (err) {
console.error(err);
} finally {
await client.close();
}
}

// Example usage:
getPopulationByCountry("Netherlands").then(res => console.log(res));
Binary file added Week4/homework/ex1-aggregation/3-AllTheINfo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions Week4/homework/ex1-aggregation/3-AllTheInformation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { MongoClient } from "mongodb";
import dotenv from "dotenv";
dotenv.config();

const client = new MongoClient(process.env.MONGO_URI);


async function getContinentPopulationByYearAndAge(year, age) {
try {
await client.connect();
const db = client.db("databaseWeek4");
const collection = db.collection("population");

const result = await collection.aggregate([
{ $match: { Year: year, Age: age } },
{ $addFields: { TotalPopulation: { $add: ["$M", "$F"] } } },
{ $sort: { Country: 1 } }
]).toArray();

return result;

} catch (err) {
console.error("Error:", err);
} finally {
await client.close();
}
}

// Example usage:
getContinentPopulationByYearAndAge(2020, "100+").then(res => console.log(res));
Binary file added Week4/homework/ex2-transaction/Transfer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions Week4/homework/ex2-transaction/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { setup } from "./setup.js";
import { transferAmount } from "./transfer.js";

async function main() {
await setup();
await transferAmount(101, 102, 1000);
}

main();
179 changes: 179 additions & 0 deletions Week4/homework/ex2-transaction/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Week4/homework/ex2-transaction/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "ex2-transaction",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^17.2.3",
"mongodb": "^7.0.0"
}
}
32 changes: 32 additions & 0 deletions Week4/homework/ex2-transaction/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { MongoClient } from "mongodb";
import dotenv from "dotenv";
dotenv.config();

const client = new MongoClient(process.env.MONGO_URI);

export async function setup() {
try {
await client.connect();
const db = client.db("databaseWeek4");
const accounts = db.collection("accounts");
const accountChanges = db.collection("account_changes");

// Clear previous data
await accounts.deleteMany({});

await accountChanges.deleteMany({});

// Insert sample accounts
await accounts.insertMany([
{ account_number: 101, balance: 5000, account_changes: [] },
{ account_number: 102, balance: 3000, account_changes: [] }
]);

console.log("Setup done: accounts created with account_changes array.");
} catch (err) {
console.error(err);
} finally {
await client.close();
}
}

Loading