forked from HackYourFuture/databases
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Muatasim-Aswad
wants to merge
2
commits into
HackYourAssignment:main
Choose a base branch
from
Muatasim-Aswad:assignment_4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
const sequenceDocument = await db | ||
.collection('counters') | ||
.findOneAndUpdate( | ||
{ _id: id }, | ||
{ $inc: { sequence_value: 1 } }, | ||
{ returnDocument: 'after' }, | ||
); | ||
|
||
return sequenceDocument.sequence_value; | ||
} | ||
|
||
export { createCounter, getNextCount }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It seems that you didn't need this function after all?
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.
@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.