From 1900a14e787abf34ee83e65149081edf54b6634e Mon Sep 17 00:00:00 2001 From: Hanna Dubyna Date: Wed, 17 Sep 2025 23:52:30 +0200 Subject: [PATCH] Completed assignment --- Week4/homework/ex1-aggregation/index.js | 63 +++++++ Week4/homework/ex2-transactions/index.js | 27 +++ Week4/homework/ex2-transactions/setup.js | 15 ++ Week4/homework/ex2-transactions/transfer.js | 51 ++++++ Week4/package-lock.json | 176 ++++++++++++++++++++ Week4/package.json | 17 ++ 6 files changed, 349 insertions(+) create mode 100644 Week4/homework/ex1-aggregation/index.js create mode 100644 Week4/homework/ex2-transactions/index.js create mode 100644 Week4/homework/ex2-transactions/setup.js create mode 100644 Week4/homework/ex2-transactions/transfer.js create mode 100644 Week4/package-lock.json create mode 100644 Week4/package.json diff --git a/Week4/homework/ex1-aggregation/index.js b/Week4/homework/ex1-aggregation/index.js new file mode 100644 index 000000000..de763fd86 --- /dev/null +++ b/Week4/homework/ex1-aggregation/index.js @@ -0,0 +1,63 @@ +require("dotenv").config({ path: "D:/HYF/databases-Cohort53/Week4/.env" }); + +const { MongoClient } = require("mongodb"); + +async function getTotalPopulationPerYear(collection, country) { + return await collection + .aggregate([ + { $match: { Country: country } }, + { + $group: { + _id: "$Year", + countPopulation: { $sum: { $add: ["$M", "$F"]} }, + }, + }, + { $sort: { _id: 1 } }, + ]) + .toArray(); +} + +async function getContinentsInfoWithTotalPopulation(collection, year, age) { + return await collection + .aggregate([ + { $match: { Year: year, Age: age } }, + { $addFields: { TotalPopulation: { $add: ["$M", "$F"] } } }, + ]) + .toArray(); +} + +async function main() { + const uri = process.env.MONGODB_URL; + const client = new MongoClient(uri); + + try { + await client.connect(); + // await client.db("admin").command({ ping: 1 }); + + const collection = client + .db("databaseWeek4") + .collection("population_pyramid"); + + const country = "Netherlands"; + const populationByYear = await getTotalPopulationPerYear( + collection, + country + ); + console.log("Total population per year for", country, populationByYear); + + const year = 2020; + const age = "100+"; + const continentsInfo = await getContinentsInfoWithTotalPopulation( + collection, + year, + age + ); + console.log( + `Continents info for year ${year} and age ${age}:`, + continentsInfo + ); + } finally { + await client.close(); + } +} +main().catch(console.error); diff --git a/Week4/homework/ex2-transactions/index.js b/Week4/homework/ex2-transactions/index.js new file mode 100644 index 000000000..3ea222d39 --- /dev/null +++ b/Week4/homework/ex2-transactions/index.js @@ -0,0 +1,27 @@ +require("dotenv").config({ path: "D:/HYF/databases-Cohort53/Week4/.env" }); + +const { MongoClient } = require("mongodb"); + +const setupAccounts = require("./setup"); +const transfer = require("./transfer"); + +async function main() { + const uri = process.env.MONGODB_URL; + const client = new MongoClient(uri); + + try { + await client.connect(); + const collection = client.db("databaseWeek4").collection("accounts"); + + await setupAccounts(collection); + + await transfer(collection, 101, 102, 1000, "Test transfer"); + + const accounts = await collection.find().toArray(); + console.log(accounts); + } finally { + await client.close(); + } +} + +main().catch(console.error); diff --git a/Week4/homework/ex2-transactions/setup.js b/Week4/homework/ex2-transactions/setup.js new file mode 100644 index 000000000..5c82bcd8a --- /dev/null +++ b/Week4/homework/ex2-transactions/setup.js @@ -0,0 +1,15 @@ +module.exports = async function setupAccounts(collection) { + await collection.deleteMany({}); + await collection.insertMany([ + { + account_number: 101, + balance: 5000, + account_changes: [], + }, + { + account_number: 102, + balance: 3000, + account_changes: [], + }, + ]); +}; diff --git a/Week4/homework/ex2-transactions/transfer.js b/Week4/homework/ex2-transactions/transfer.js new file mode 100644 index 000000000..e6b734239 --- /dev/null +++ b/Week4/homework/ex2-transactions/transfer.js @@ -0,0 +1,51 @@ +module.exports = async function transfer( + collection, + fromAcc, + toAcc, + amount, + remark +) { + const fromAccount = await collection.findOne({ account_number: fromAcc }); + const toAccount = await collection.findOne({ account_number: toAcc }); + + if (!fromAccount || !toAccount) { + throw new Error("One of the accounts does not exist"); + } + + if (fromAccount.balance < amount) { + throw new Error("Insufficient funds"); + } + + const fromChanges = fromAccount.account_changes || []; + const toChanges = toAccount.account_changes || []; + + await collection.updateOne( + { account_number: fromAcc }, + { + $inc: { balance: -amount }, + $push: { + account_changes: { + change_number: fromChanges.length + 1, + amount: -amount, + changed_date: new Date(), + remark, + }, + }, + } + ); + + await collection.updateOne( + { account_number: toAcc }, + { + $inc: { balance: amount }, + $push: { + account_changes: { + change_number: toChanges.length + 1, + amount: amount, + changed_date: new Date(), + remark, + }, + }, + } + ); +}; diff --git a/Week4/package-lock.json b/Week4/package-lock.json new file mode 100644 index 000000000..7a3e9415a --- /dev/null +++ b/Week4/package-lock.json @@ -0,0 +1,176 @@ +{ + "name": "week4", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "week4", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "dotenv": "^17.2.2", + "mongodb": "^6.19.0" + } + }, + "node_modules/@mongodb-js/saslprep": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.3.0.tgz", + "integrity": "sha512-zlayKCsIjYb7/IdfqxorK5+xUMyi4vOKcFy10wKJYc63NSdKI8mNME+uJqfatkPmOSMMUiojrL58IePKBm3gvQ==", + "license": "MIT", + "dependencies": { + "sparse-bitfield": "^3.0.3" + } + }, + "node_modules/@types/webidl-conversions": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz", + "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA==", + "license": "MIT" + }, + "node_modules/@types/whatwg-url": { + "version": "11.0.5", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.5.tgz", + "integrity": "sha512-coYR071JRaHa+xoEvvYqvnIHaVqaYrLPbsufM9BF63HkwI5Lgmy2QR8Q5K/lYDYo5AK82wOvSOS0UsLTpTG7uQ==", + "license": "MIT", + "dependencies": { + "@types/webidl-conversions": "*" + } + }, + "node_modules/bson": { + "version": "6.10.4", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.10.4.tgz", + "integrity": "sha512-WIsKqkSC0ABoBJuT1LEX+2HEvNmNKKgnTAyd0fL8qzK4SH2i9NXg+t08YtdZp/V9IZ33cxe3iV4yM0qg8lMQng==", + "license": "Apache-2.0", + "engines": { + "node": ">=16.20.1" + } + }, + "node_modules/dotenv": { + "version": "17.2.2", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.2.2.tgz", + "integrity": "sha512-Sf2LSQP+bOlhKWWyhFsn0UsfdK/kCWRv1iuA2gXAwt3dyNabr6QSj00I2V10pidqz69soatm9ZwZvpQMTIOd5Q==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, + "node_modules/memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "license": "MIT" + }, + "node_modules/mongodb": { + "version": "6.19.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.19.0.tgz", + "integrity": "sha512-H3GtYujOJdeKIMLKBT9PwlDhGrQfplABNF1G904w6r5ZXKWyv77aB0X9B+rhmaAwjtllHzaEkvi9mkGVZxs2Bw==", + "license": "Apache-2.0", + "dependencies": { + "@mongodb-js/saslprep": "^1.1.9", + "bson": "^6.10.4", + "mongodb-connection-string-url": "^3.0.0" + }, + "engines": { + "node": ">=16.20.1" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.188.0", + "@mongodb-js/zstd": "^1.1.0 || ^2.0.0", + "gcp-metadata": "^5.2.0", + "kerberos": "^2.0.1", + "mongodb-client-encryption": ">=6.0.0 <7", + "snappy": "^7.3.2", + "socks": "^2.7.1" + }, + "peerDependenciesMeta": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "@mongodb-js/zstd": { + "optional": true + }, + "gcp-metadata": { + "optional": true + }, + "kerberos": { + "optional": true + }, + "mongodb-client-encryption": { + "optional": true + }, + "snappy": { + "optional": true + }, + "socks": { + "optional": true + } + } + }, + "node_modules/mongodb-connection-string-url": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.2.tgz", + "integrity": "sha512-rMO7CGo/9BFwyZABcKAWL8UJwH/Kc2x0g72uhDWzG48URRax5TCIcJ7Rc3RZqffZzO/Gwff/jyKwCU9TN8gehA==", + "license": "Apache-2.0", + "dependencies": { + "@types/whatwg-url": "^11.0.2", + "whatwg-url": "^14.1.0 || ^13.0.0" + } + }, + "node_modules/punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", + "license": "MIT", + "dependencies": { + "memory-pager": "^1.0.2" + } + }, + "node_modules/tr46": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-5.1.1.tgz", + "integrity": "sha512-hdF5ZgjTqgAntKkklYw0R03MG2x/bSzTtkxmIRw/sTNV8YXsCJ1tfLAX23lhxhHJlEf3CRCOCGGWw3vI3GaSPw==", + "license": "MIT", + "dependencies": { + "punycode": "^2.3.1" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + } + }, + "node_modules/whatwg-url": { + "version": "14.2.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-14.2.0.tgz", + "integrity": "sha512-De72GdQZzNTUBBChsXueQUnPKDkg/5A5zp7pFDuQAj5UFoENpiACU0wlCvzpAGnTkj++ihpKwKyYewn/XNUbKw==", + "license": "MIT", + "dependencies": { + "tr46": "^5.1.0", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=18" + } + } + } +} diff --git a/Week4/package.json b/Week4/package.json new file mode 100644 index 000000000..d927d7f4e --- /dev/null +++ b/Week4/package.json @@ -0,0 +1,17 @@ +{ + "name": "week4", + "version": "1.0.0", + "description": "These are the topics for week 4, all in MongoDB:", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "dependencies": { + "dotenv": "^17.2.2", + "mongodb": "^6.19.0" + } +}