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 week 4 Asim #44

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
83 changes: 83 additions & 0 deletions Week4/homework/ex1-aggregation/aggregation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { MongoClient } from 'mongodb';

async function main() {
if (process.env.MONGODB_URI == null) throw Error(`no uri in .env`);
const client = new MongoClient(process.env.MONGODB_URI);
await client.connect();

const collection = client
.db('population_pyramid_1950-2022')
.collection('population_pyramid_1950-2022');

const result1 = await populationByYear('Netherlands', collection);
const result2 = await continentsPopulation(2020, '100+', collection);

console.log([result1, result2]);
}

main()
.catch(console.error)
.finally(() => client.close());

function populationByYear(country, collection) {
const pipeline = [
{
$match: {
Country: country,
},
},
{
$group: {
_id: '$Year',
countPopulation: {
$sum: {
$add: ['$F', '$M'],
},
},
},
},
{
$sort: {
_id: 1,
},
},
];

return collection.aggregate(pipeline).toArray();
}

function continentsPopulation(year, age, collection) {
const pipeline = [
{
$match: {
Country: {
$in: [
'ASIA',
'AFRICA',
'EUROPE',
'NORTHERN AMERICA',
'LATIN AMERICA AND THE CARIBBEAN',
'OCEANIA',
'AUSTRALIA',
],
},
Year: year,
Age: age,
},
},
{
$set: {
TotalPopulation: {
$sum: ['$M', '$F'],
},
},
},
{
$sort: {
Country: 1,
},
},
];

return collection.aggregate(pipeline).toArray();
}
69 changes: 69 additions & 0 deletions Week4/homework/ex1-aggregation/log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[
[
{ _id: 1950, countPopulation: 10042051 },
{ _id: 1960, countPopulation: 11448815 },
{ _id: 1970, countPopulation: 13001941 },
{ _id: 1980, countPopulation: 14148410 },
{ _id: 1990, countPopulation: 14965442 },
{ _id: 2000, countPopulation: 15926188 },
{ _id: 2010, countPopulation: 16682925 },
{ _id: 2020, countPopulation: 17134872 },
{ _id: 2022, countPopulation: 17211448 }
],
[
{
_id: new ObjectId('6727efac95e737a958badd58'),
Country: 'AFRICA',
Year: 2020,
Age: '100+',
M: 1327,
F: 2723,
TotalPopulation: 4050
},
{
_id: new ObjectId('6727efac95e737a958bade00'),
Country: 'ASIA',
Year: 2020,
Age: '100+',
M: 57019,
F: 207883,
TotalPopulation: 264902
},
{
_id: new ObjectId('6727efad95e737a958bae301'),
Country: 'EUROPE',
Year: 2020,
Age: '100+',
M: 22579,
F: 102056,
TotalPopulation: 124635
},
{
_id: new ObjectId('6727efad95e737a958bae634'),
Country: 'LATIN AMERICA AND THE CARIBBEAN',
Year: 2020,
Age: '100+',
M: 19858,
F: 49218,
TotalPopulation: 69076
},
{
_id: new ObjectId('6727efad95e737a958bae9a6'),
Country: 'NORTHERN AMERICA',
Year: 2020,
Age: '100+',
M: 22267,
F: 83419,
TotalPopulation: 105686
},
{
_id: new ObjectId('6727efad95e737a958bae9e5'),
Country: 'OCEANIA',
Year: 2020,
Age: '100+',
M: 1094,
F: 3980,
TotalPopulation: 5074
}
]
]
48 changes: 48 additions & 0 deletions Week4/homework/ex2-transactions/dummyData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export default function () {
const accounts2D = [
[101, 3000.0],
[102, 2000.0],
[103, 5000.0],
[104, 100.0],
[105, 1500.0],
[106, 177.0],
[107, 11589.0],
[108, 1020.0],
[109, 2024.0],
[110, 1000.0],
];

const changes2D = [
[101, 100.0, '2020-01-01', 'Deposit'],
[102, 200.0, '2020-01-02', 'Withdraw'],
[103, 300.0, '2020-01-03', 'Deposit'],
[104, 400.0, '2020-01-04', 'Withdraw'],
[105, 500.0, '2020-01-05', 'Deposit'],
];

//convert accounts data into objects
const accounts = accounts2D.map(([account_number, balance]) => ({
account_number,
balance,
account_changes: [],
}));

//add changes logs into their accounts
changes2D.forEach(([account_number, amount, changed_date, remark]) => {
const change_number = 1;

const log = {
change_number,
account_number,
amount,
changed_date: new Date(changed_date),
remark,
};

accounts
.find((account) => account.account_number === account_number)
.account_changes.push(log);
});

return accounts;
}
20 changes: 20 additions & 0 deletions Week4/homework/ex2-transactions/getNextChangeNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function createCounter(db, id) {
return db.collection('counters').insertOne({
_id: id,
sequence_value: 0,
});
}

async function getNextCount(db, id) {

Choose a reason for hiding this comment

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

It seems that you didn't need this function after all?

Copy link
Author

Choose a reason for hiding this comment

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

@dyaboykyl In version 1, I referenced account-changes and used it. However, I didn't delete it when I rewrote the embedded version as it seems a pattern I might use in another place.

Thank you for all the valuable feedback and info.

const sequenceDocument = await db
.collection('counters')
.findOneAndUpdate(
{ _id: id },
{ $inc: { sequence_value: 1 } },
{ returnDocument: 'after' },
);

return sequenceDocument.sequence_value;
}

export { createCounter, getNextCount };
63 changes: 63 additions & 0 deletions Week4/homework/ex2-transactions/log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Success!

** Result:
[
{
acknowledged: true,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0,
matchedCount: 1
},
{
acknowledged: true,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0,
matchedCount: 1
}
]
[
{
"_id": "6728ec71c748cebad1117f25",
"account_number": 101,
"balance": 2740,
"account_changes": [
{
"change_number": 1,
"account_number": 101,
"amount": 100,
"changed_date": "2020-01-01T00:00:00.000Z",
"remark": "Deposit"
},
{
"change_number": 2,
"account_number": 101,
"amount": -260,
"changed_date": "2024-11-04T15:46:58.163Z",
"remark": "transfer to account 102"
}
]
},
{
"_id": "6728ec71c748cebad1117f26",
"account_number": 102,
"balance": 2260,
"account_changes": [
{
"change_number": 1,
"account_number": 102,
"amount": 200,
"changed_date": "2020-01-02T00:00:00.000Z",
"remark": "Withdraw"
},
{
"change_number": 2,
"account_number": 102,
"amount": 260,
"changed_date": "2024-11-04T15:46:58.232Z",
"remark": "transfer from account 101"
}
]
}
]
15 changes: 15 additions & 0 deletions Week4/homework/ex2-transactions/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import getData from './dummyData.js';

export default async function setup(client) {
//drop if exists and recreate
const db = client.db('account_db');
await db.dropDatabase();
const account = db.collection('account');

//index
await account.createIndex({ account_number: 1 }, { unique: true });

//populate
const accounts = getData(db);
return account.insertMany(accounts);
}
Loading