From 30de3da56372a302cb50b7f8b8adcca9fb579cfc Mon Sep 17 00:00:00 2001 From: Ilias Khugaev Date: Tue, 10 Jun 2025 13:26:25 +0200 Subject: [PATCH 1/2] finale commit --- Week4/homework/mongoDB-Connection.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Week4/homework/mongoDB-Connection.js diff --git a/Week4/homework/mongoDB-Connection.js b/Week4/homework/mongoDB-Connection.js new file mode 100644 index 000000000..e69de29bb From ecbea5afad8adb035541225d2f815f667f241964 Mon Sep 17 00:00:00 2001 From: Ilias Khugaev Date: Wed, 18 Jun 2025 16:16:34 +0200 Subject: [PATCH 2/2] last commit --- Week4/homework/Aggregation.js | 81 ++++++++++ Week4/homework/ex2-transactions/setup.js | 22 +++ Week4/homework/index.js | 21 +++ Week4/homework/mongoDB-Connection.js | 26 ++++ Week4/homework/package-lock.json | 190 +++++++++++++++++++++++ Week4/homework/package.json | 19 +++ Week4/homework/transfer.js | 49 ++++++ 7 files changed, 408 insertions(+) create mode 100644 Week4/homework/Aggregation.js create mode 100644 Week4/homework/ex2-transactions/setup.js create mode 100644 Week4/homework/index.js create mode 100644 Week4/homework/package-lock.json create mode 100644 Week4/homework/package.json create mode 100644 Week4/homework/transfer.js diff --git a/Week4/homework/Aggregation.js b/Week4/homework/Aggregation.js new file mode 100644 index 000000000..ac01cc47c --- /dev/null +++ b/Week4/homework/Aggregation.js @@ -0,0 +1,81 @@ +import { MongoClient } from 'mongodb' + + +export const uri = 'mongodb+srv://Ilias:zePnKS09BRPZBwaW@cluster0.w5e36f8.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0'; +const client = new MongoClient(uri); + +async function run() { + try { + // Connect the client to the server (optional starting in v4.7) + await client.connect(); + PopulationPerYearInCountry(client, "Netherlands"); + findByYearAndAge(client, 1950, "100+"); + + await client.db("admin").command( { ping: 1 }) + console.log("Pinged your deployment. You successfully connected to MongoDB!"); + } finally { + // Ensures that the client will close when you finish/error + await client.close(); + } +} +run().catch(console.dir); + +const findByNameOfCity = async (client, cityName) => { + const result = await client.db("databaseWeek4").collection("population_pyramid_1950-2022").find({Country: cityName}); + console.log(result); +} + +const PopulationPerYearInCountry = async (client, countryName) => { + const pipeline = [ + { + '$match': { + 'Country': 'Afghanistan' + } + }, { + '$group': { + '_id': '$Year', + 'countPopulation': { + '$sum': { + '$add': [ + '$M', '$F' + ] + } + } + } + }, { + '$sort': { + '_id': -1 + } + } + ]; + const result = await client.db("databaseWeek4").collection("population_pyramid_1950-2022").aggregate(pipeline).toArray();; + console.table(result); +} + +const findByYearAndAge = async (client, year, age) => { + const pipeline = [ + { + '$match': { + 'Country': { + '$regex': '^[A-Z]+$' + } + } + }, { + '$match': { + 'Age': age, + 'Year': year + } + }, { + '$addFields': { + 'TotalPopulation': { + '$add': [ + '$M', '$F' + ] + } + } + } + ]; + + const result = await client.db("databaseWeek4").collection("population_pyramid_1950-2022").aggregate(pipeline).toArray(); + console.table(result); +} \ No newline at end of file diff --git a/Week4/homework/ex2-transactions/setup.js b/Week4/homework/ex2-transactions/setup.js new file mode 100644 index 000000000..c4e666d19 --- /dev/null +++ b/Week4/homework/ex2-transactions/setup.js @@ -0,0 +1,22 @@ +import { MongoClient } from 'mongodb'; +import { uri } from "../Aggregation.js"; +import uuid4 from "uuid4"; + +const client = new MongoClient(uri); + +export const deleteAccount = async (client) => { + await client.db("databaseWeek4").collection("account").deleteMany({}); + console.log("Collection 'account' deleted"); +}; + +export const setUpDatabase = async (client, amountNumber, amount) => { + const account = { + accountNumber: amountNumber, + balance: amount, + account_changes: [] + }; + + await client.db("databaseWeek4").collection("account").insertOne(account); + + console.log(`Inserted account with number: ${account.accountNumber}`); +}; diff --git a/Week4/homework/index.js b/Week4/homework/index.js new file mode 100644 index 000000000..37386e3cd --- /dev/null +++ b/Week4/homework/index.js @@ -0,0 +1,21 @@ +import { MongoClient } from 'mongodb'; +import {transactionsDitals, transactions} from "./transfer.js"; +import {deleteAccount, setUpDatabase} from "./ex2-transactions/setup.js"; + + + +export const uri = 'mongodb+srv://Ilias:zePnKS09BRPZBwaW@cluster0.w5e36f8.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0'; +const client = new MongoClient(uri); + + +try { + // deleteAccount(); + // setUpDatabase() + await transactions(client, 103, 101, 500, "Gift"); + + +} catch (error) { + console.error(error) +} finally { + await client.close(); +} diff --git a/Week4/homework/mongoDB-Connection.js b/Week4/homework/mongoDB-Connection.js index e69de29bb..4038c0272 100644 --- a/Week4/homework/mongoDB-Connection.js +++ b/Week4/homework/mongoDB-Connection.js @@ -0,0 +1,26 @@ + +const { MongoClient, ServerApiVersion } = require('mongodb'); +const uri = "mongodb+srv://Naprimer:A4gIhLLcDW6KsYmr@cluster0.w5e36f8.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"; + +// Create a MongoClient with a MongoClientOptions object to set the Stable API version +const client = new MongoClient(uri, { + serverApi: { + version: ServerApiVersion.v1, + strict: true, + deprecationErrors: true, + } +}); + +async function run() { + try { + // Connect the client to the server (optional starting in v4.7) + await client.connect(); + // Send a ping to confirm a successful connection + await client.db("admin").command({ ping: 1 }); + console.log("Pinged your deployment. You successfully connected to MongoDB!"); + } finally { + // Ensures that the client will close when you finish/error + await client.close(); + } +} +run().catch(console.dir); diff --git a/Week4/homework/package-lock.json b/Week4/homework/package-lock.json new file mode 100644 index 000000000..9b825e651 --- /dev/null +++ b/Week4/homework/package-lock.json @@ -0,0 +1,190 @@ +{ + "name": "homework", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "homework", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "csv-parser": "^3.2.0", + "mongodb": "^6.17.0", + "pandas": "^0.0.3", + "uuid4": "^2.0.3" + } + }, + "node_modules/@mongodb-js/saslprep": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.2.2.tgz", + "integrity": "sha512-EB0O3SCSNRUFk66iRCpI+cXzIjdswfCs7F6nOC3RAGJ7xr5YhaicvsRwJ9eyzYvYRlCSDUO/c7g4yNulxKC1WA==", + "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/csv-parser": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/csv-parser/-/csv-parser-3.2.0.tgz", + "integrity": "sha512-fgKbp+AJbn1h2dcAHKIdKNSSjfp43BZZykXsCjzALjKy80VXQNHPFJ6T9Afwdzoj24aMkq8GwDS7KGcDPpejrA==", + "license": "MIT", + "bin": { + "csv-parser": "bin/csv-parser" + }, + "engines": { + "node": ">= 10" + } + }, + "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.17.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.17.0.tgz", + "integrity": "sha512-neerUzg/8U26cgruLysKEjJvoNSXhyID3RvzvdcpsIi2COYM3FS3o9nlH7fxFtefTb942dX3W9i37oPfCVj4wA==", + "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.2.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/pandas": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/pandas/-/pandas-0.0.3.tgz", + "integrity": "sha512-b6b0XOuj8mvj2Kn6qfcvPmb0ILCcbuep5VYZche3730P9cXQcvarbbvWJxwXkxc3OXyGFYjJ4kozjX5ZXXMrsw==", + "license": "WTFPL" + }, + "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/uuid4": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/uuid4/-/uuid4-2.0.3.tgz", + "integrity": "sha512-CTpAkEVXMNJl2ojgtpLXHgz23dh8z81u6/HEPiQFOvBc/c2pde6TVHmH4uwY0d/GLF3tb7+VDAj4+2eJaQSdZQ==", + "license": "ISC" + }, + "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/homework/package.json b/Week4/homework/package.json new file mode 100644 index 000000000..6c83f257c --- /dev/null +++ b/Week4/homework/package.json @@ -0,0 +1,19 @@ +{ + "name": "homework", + "version": "1.0.0", + "main": "mongoDB-Connection.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "csv-parser": "^3.2.0", + "mongodb": "^6.17.0", + "pandas": "^0.0.3", + "uuid4": "^2.0.3" + }, + "keywords": [], + "description": "" +} diff --git a/Week4/homework/transfer.js b/Week4/homework/transfer.js new file mode 100644 index 000000000..b19f0e77b --- /dev/null +++ b/Week4/homework/transfer.js @@ -0,0 +1,49 @@ +import uuid4 from "uuid4"; + + + +export const transactionsDitals = (amount, change_number, remark) => { + return { + amount, + change_number, + changed_date: new Date(), + remark: remark + + }; +}; + +export const transactions = async (client, from, to, amount, remark) => { + const accounts = client.db("databaseWeek4").collection("account"); + const session = client.startSession(); + const change_number = uuid4(); + + try { + await session.withTransaction(async () => { + await accounts.updateOne( + { accountNumber: from }, + { $inc: { balance: -amount } }, + { session } + ); + await accounts.updateOne( + { accountNumber: from }, + { $push: { account_changes: transactionsDitals(amount, change_number, remark)} }, + { session } + ); + await accounts.updateOne( + { accountNumber: to }, + { $inc: { balance: amount } }, + { session } + ); + await accounts.updateOne( + { accountNumber: to }, + { $push: { account_changes: transactionsDitals(amount, change_number, remark)} }, + { session } + ); + }); + console.log("Transaction completed successfully"); + } catch (e) { + console.error("Transaction aborted due to an error:", e); + } finally { + await session.endSession(); + } +}; \ No newline at end of file