-
Notifications
You must be signed in to change notification settings - Fork 3
Ilias_Khugaev-w4-Databases #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]+$' | ||
|
Comment on lines
+59
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: interesting way to match continent name and it is correct. However, in the real working situation we tend to avoid using REGEX as it is slow. The performance is important especially when you have a lot of requests per second coming to your backend service. Continents are fixed and don't have a lot of values, so you can use |
||
| } | ||
| } | ||
| }, { | ||
| '$match': { | ||
| 'Age': age, | ||
| 'Year': year | ||
| } | ||
|
Comment on lines
+63
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Why don't you merge these two |
||
| }, { | ||
| '$addFields': { | ||
| 'TotalPopulation': { | ||
| '$add': [ | ||
| '$M', '$F' | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| const result = await client.db("databaseWeek4").collection("population_pyramid_1950-2022").aggregate(pipeline).toArray(); | ||
| console.table(result); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}`); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
|
Comment on lines
+14
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use this file to test connection but in the real working environment, you don't need to upload this file to git. |
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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": "" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: It is not a good practice to store your URL/keys in the code. Better to have an environment variable file : )