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
20 changes: 20 additions & 0 deletions Week4/assignment/Aggregation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Node modules
node_modules/

# Logs
*.log

# Environment variables
.env

# Build output
dist/
build/

# OS files
.DS_Store
Thumbs.db

# IDE files
.vscode/
.idea/
94 changes: 94 additions & 0 deletions Week4/assignment/Aggregation/ex1-aggregation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { MongoClient } from "mongodb";
import dotenv from "dotenv"

dotenv.config()

const url = process.env.MONGODB_URL
if(!url) throw new Error("MONGODB_URL is not set in .env")
const client = new MongoClient(url);
const db = client.db("databaseWeek4");
const collection = db.collection("week4Exercises");

/*Write a function that will return the array of the total population (M + F over all age groups) for a given Country per year.
The result should look something like this, these are the values for Netherlands:*/

async function getTotalPopulationByCountryPerYear() {
try {
// Example query: find first 1 document
const docs = await collection.find().limit(1).toArray();
console.log(docs);

const pipeline = [
{ $match: { Country: "Netherlands" } },
{
$group: {
_id: "$Year",
countPopulation: { $sum: { $add: ["$M", "$F"] } },
},
},
{ $sort: { _id: 1 } },
];

const result = await collection.aggregate(pipeline).toArray();
console.log(result);

const formated = result.map((item) => {
return {
Year: item._id,
countPopulation: item.countPopulation,
};
});
console.table(formated);
} catch (err) {
console.error(err);
}
}

/*Write a function that will return all the information of each continent for a given Year
and Age field but add a new field TotalPopulation that will be the addition of M and F.
For example, if I would give 2020 for the Year and 100+ for the Age it should return something like this:*/
async function getContinentPopulation(year, age) {
try {
const continents = [
"AFRICA",
"ASIA",
"EUROPE",
"LATIN AMERICA AND THE CARIBBEAN",
"NORTHERN AMERICA",
"OCEANIA",
];
const pipeline = [
{
$match: { Year: year, Age: age, Country: { $in: continents } },
},
{
$addFields: {
TotalPopulation: { $add: ["$M", "$F"] },
},
},
];

const result = await collection.aggregate(pipeline).toArray();
return result;
} catch (error) {
console.error("Error", error);
}
}

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

const data = await getContinentPopulation(2020, "100+");
console.log(data);
console.table(data);
} catch (err) {
console.error(err);
} finally {
await client.close();
}
}

main();
176 changes: 176 additions & 0 deletions Week4/assignment/Aggregation/package-lock.json

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

18 changes: 18 additions & 0 deletions Week4/assignment/Aggregation/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "aggregation",
"version": "1.0.0",
"description": "",
"main": "ex1-aggregation.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ex1-aggregation.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^17.2.2",
"mongodb": "^6.19.0"
}
}
20 changes: 20 additions & 0 deletions Week4/assignment/Transaction/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Node modules
node_modules/

# Logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Environment variables
.env

# OS files
.DS_Store
Thumbs.db

# Optional: Build output
dist/
build/
12 changes: 12 additions & 0 deletions Week4/assignment/Transaction/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MongoClient } from "mongodb";
import dotenv from "dotenv";

dotenv.config();

const MONGODB_URL = process.env.MONGODB_URL;
if (!MONGODB_URL) throw new Error("MONGODB_URL is not defined in .env");
const client = new MongoClient(MONGODB_URL);
const db = client.db("Transfer");
const transactionsCollection = db.collection("transactions");

export { client, transactionsCollection };
20 changes: 20 additions & 0 deletions Week4/assignment/Transaction/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { client } from "./db.js";
import { createAccounts, showAccounts } from "./setup.js";
import { transferMoney } from "./transfer.js";

async function connectToDatabase() {
try {
await client.connect();
console.log(`Successfully connected to MongoDB`);
await createAccounts();
await showAccounts();
await transferMoney(102, 103, 900);
await showAccounts();
} catch (error) {
console.error("Error:", error);
} finally {
await client.close();
}
}

connectToDatabase();
Loading