Skip to content
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

Assignment-week4-databases-AliaTaher #53

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
90 changes: 90 additions & 0 deletions Week4/homework/exercise1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { MongoClient } from "mongodb";
import "dotenv/config";

const DATABASE_NAME = "databaseWeek4";
const COLLECTION_NAME = "population";
Comment on lines +4 to +5
Copy link

Choose a reason for hiding this comment

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

Nice 👍

const client = new MongoClient(process.env.MONGODB_URL);

const getCollection = (client) => {
return client.db(DATABASE_NAME).collection(COLLECTION_NAME);
};

const getPopulationByCountry = async (client, country) => {
try {
const result = await getCollection(client)
.aggregate([
{ $match: { Country: country } },
{
$group: {
_id: "$Year",
populationCount: { $sum: { $add: ["$M", "$F"] } },
},
},
{ $sort: { _id: 1 } },
])
.toArray();

console.log(result);
} catch (error) {
console.error(error);
}
};

const continentsInformation = async (client, year, age) => {
try {
const result = await getCollection(client)
.aggregate([
{
$match: {
Year: year,
Age: age,
Country: {
$in: [
"AFRICA",
"ASIA",
"EUROPE",
"LATIN AMERICA AND THE CARIBBEAN",
"NORTHERN AMERICA",
"OCEANIA",
],
},
},
},
{
$project: {
_id: 1,
Country: 1,
Year: 1,
Age: 1,
M: 1,
F: 1,
TotalPopulation: {
$add: ["$M", "$F"],
},
},
},
{
$sort: { Country: 1 },
},
])
.toArray();

console.log(result);
} catch (error) {
console.error(error);
}
};

const main = async () => {
try {
await client.connect();
await getPopulationByCountry(client, "Netherlands");
await continentsInformation(client, 2020, "100+");
} catch (error) {
console.error("Error: ", error);
} finally {
await client.close();
}
};

main();
16 changes: 16 additions & 0 deletions Week4/homework/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { setupDatabase ,CLIENT} from "./setup.js";
import { transfer } from "./transfer.js";

const main = async () => {
try {
await setupDatabase();
await transfer("101", "102", 1000, "Transfer");
} catch (error) {
console.error(error);
}finally{
if(CLIENT.connect()){
await CLIENT.close();
}
}
};
main();
176 changes: 176 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.

17 changes: 17 additions & 0 deletions Week4/homework/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "homework",
"version": "1.0.0",
"main": "exercise1.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"type": "module",
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"dotenv": "^16.4.5",
"mongodb": "^6.10.0"
}
}
80 changes: 80 additions & 0 deletions Week4/homework/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { MongoClient } from "mongodb";
import "dotenv/config";

export const DATABASE_NAME = "databaseWeek4";
export const COLLECTION_NAME = "accounts";
export const CLIENT = new MongoClient(process.env.MONGODB_URL);

export const setupDatabase = async () => {
try {
await CLIENT.connect();
const collection = CLIENT.db(DATABASE_NAME).collection(COLLECTION_NAME);

await collection.deleteMany({});
console.log("Accounts collection cleaned up.");

const sampleData = [
{
account_number: "101",
balance: 4000,
account_changes: [
{
change_number: 1,
amount: 500,
changed_date: new Date(),
remark: "deposit",
},
{
change_number: 2,
amount: -100,
changed_date: new Date(),
remark: "withdrawal",
},
],
},
{
account_number: "102",
balance: 9000,
account_changes: [
{
change_number: 1,
amount: 1000,
changed_date: new Date(),
remark: "deposit",
},
{
change_number: 2,
amount: -200,
changed_date: new Date(),
remark: "withdrawal",
},
],
},
{
account_number: "103",
balance: 8000,
account_changes: [
{
change_number: 1,
amount: 1500,
changed_date: new Date(),
remark: "deposit",
},
{
change_number: 2,
amount: -300,
changed_date: new Date(),
remark: "withdrawal",
},
],
},
];

const result = await collection.insertMany(sampleData);
console.log(
`Inserted ${result.insertedCount} accounts into the collection.`
);
} catch (error) {
console.error("Error setting up the database:", error);
}
};
Loading