-
Notifications
You must be signed in to change notification settings - Fork 3
Rizan_ibrahim-w4-database #31
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
base: main
Are you sure you want to change the base?
Rizan_ibrahim-w4-database #31
Conversation
yunchen4
left a comment
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.
Hi Rizan,
There are two points you need to rework, and I have made them explicit. Please let me know on Slack after you finish the rework!
| const result = await collection | ||
| .aggregate([ | ||
| { $match: { Year: year, Age: age } }, | ||
| { | ||
| $addFields: { | ||
| TotalPopulation: { $add: ["$M", "$F"] }, | ||
| }, | ||
| }, | ||
| ]) | ||
| .toArray(); |
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.
Needs rework: In the CSV file you can see continents are mixed with countries, for example there are documents for AFRICA. In this case you cannot just add all documents.
But I have to say it is not a good dataset 😅
| export async function transfer(fromAcc, toAcc, amount, remark) { | ||
| try { | ||
| await client.connect(); | ||
| const db = client.db(dbName); | ||
| const accounts = db.collection("accounts"); | ||
|
|
||
| // Fetch both accounts | ||
| const sender = await accounts.findOne({ account_number: fromAcc }); | ||
| const receiver = await accounts.findOne({ account_number: toAcc }); | ||
|
|
||
| if (!sender || !receiver) { | ||
| throw new Error("One or both accounts not found."); | ||
| } | ||
|
|
||
| if (sender.balance < amount) { | ||
| throw new Error("Insufficient funds."); | ||
| } | ||
|
|
||
| const nextSenderChange = sender.account_changes.length + 1; | ||
| const nextReceiverChange = receiver.account_changes.length + 1; | ||
|
|
||
| await accounts.updateOne( | ||
| { account_number: fromAcc }, | ||
| { | ||
| $inc: { balance: -amount }, | ||
| $push: { | ||
| account_changes: { | ||
| change_number: nextSenderChange, | ||
| amount: -amount, | ||
| changed_date: new Date(), | ||
| remark: `Sent to ${toAcc}: ${remark}`, | ||
| }, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| await accounts.updateOne( | ||
| { account_number: toAcc }, | ||
| { | ||
| $inc: { balance: amount }, | ||
| $push: { | ||
| account_changes: { | ||
| change_number: nextReceiverChange, | ||
| amount: amount, | ||
| changed_date: new Date(), | ||
| remark: `Received from ${fromAcc}: ${remark}`, | ||
| }, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| console.log(`Transfer of ${amount} from ${fromAcc} to ${toAcc} completed.`); | ||
| } catch (err) { | ||
| console.error("Transfer failed:", err.message); | ||
| } finally { | ||
| await client.close(); | ||
| } |
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.
Needs rework: I didn't see you're using transactions here?
| const continents = [ | ||
| "AFRICA", | ||
| "ASIA", | ||
| "EUROPE", | ||
| "LATIN AMERICA AND THE CARIBBEAN", | ||
| "NORTHERN AMERICA", | ||
| "OCEANIA", | ||
| ]; |
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.
Good to filter them out 😄 It solves the problem from the origin.
No description provided.