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
81 changes: 81 additions & 0 deletions Week4/homework/Aggregation.js
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);
Comment on lines +4 to +5

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 : )


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

Choose a reason for hiding this comment

The 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 IN here.

}
}
}, {
'$match': {
'Age': age,
'Year': year
}
Comment on lines +63 to +67

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Why don't you merge these two $match into one?

}, {
'$addFields': {
'TotalPopulation': {
'$add': [
'$M', '$F'
]
}
}
}
];

const result = await client.db("databaseWeek4").collection("population_pyramid_1950-2022").aggregate(pipeline).toArray();
console.table(result);
}
22 changes: 22 additions & 0 deletions Week4/homework/ex2-transactions/setup.js
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}`);
};
21 changes: 21 additions & 0 deletions Week4/homework/index.js
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();
}
26 changes: 26 additions & 0 deletions Week4/homework/mongoDB-Connection.js
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

Choose a reason for hiding this comment

The 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.

190 changes: 190 additions & 0 deletions Week4/homework/package-lock.json

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

19 changes: 19 additions & 0 deletions Week4/homework/package.json
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": ""
}
Loading