-
Notifications
You must be signed in to change notification settings - Fork 9
hAlaa nahser w4 databases #39
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| const { MongoClient, ServerApiVersion } = require("mongodb"); | ||
| require("dotenv").config(); | ||
|
|
||
| const uri = process.env.MONGODB_URL; | ||
|
|
||
| // Create a MongoClient with a MongoClientOptions object to set the Stable API version | ||
| const client = new MongoClient(uri, { | ||
| serverApi: { | ||
| version: ServerApiVersion.v1, | ||
| strict: true, | ||
| deprecationErrors: true, | ||
| }, | ||
| }); | ||
|
|
||
| async function run() { | ||
| try { | ||
| // Connect the client to the server (optional starting in v4.7) | ||
| await client.connect(); | ||
| // Send a ping to confirm a successful connection | ||
| await client.db("databaseWeek4").command({ ping: 1 }); | ||
| console.log( | ||
| "Pinged your deployment. You successfully connected to MongoDB!" | ||
| ); | ||
|
|
||
| const mongoClient = client | ||
| .db("databaseWeek4") | ||
| .collection("population_pyramid_1950-2022"); | ||
|
|
||
| console.log(await getPopulationPerCountry(mongoClient, "Netherlands")); | ||
|
|
||
| console.log(await getContinentInfo(mongoClient, 1990, "100+")); | ||
| } finally { | ||
| await client.close(); | ||
| } | ||
| } | ||
| run().catch(console.dir); | ||
|
|
||
| const getPopulationPerCountry = async (mongoClient, country) => { | ||
| const pipeline = [ | ||
| { | ||
| $match: { | ||
| Country: country, | ||
| }, | ||
| }, | ||
| { | ||
| $group: { | ||
| _id: "$Year", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a data import file in your PR. Can you explain how you got your data into your db? From the README:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I imported the CSV file directly into MongoDB Atlas using the Atlas import tools. Was I supposed to handle the data import via Node.js instead? |
||
| countPopulation: { | ||
| $sum: { | ||
| $add: ["$M", "$F"], | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| $project: { | ||
| _id: 0, | ||
| Year: "$_id", | ||
| countPopulation: 1, | ||
| }, | ||
| }, | ||
| { | ||
| $sort: { | ||
| Year: 1, | ||
| }, | ||
| }, | ||
| ]; //end pipeline | ||
|
|
||
| return await mongoClient.aggregate(pipeline).toArray(); | ||
| }; | ||
|
|
||
| const getContinentInfo = async (mongoClient, year, age) => { | ||
| const pipeline = [ | ||
| { | ||
| $match: { | ||
| Country: { | ||
| $in: [ | ||
| "AFRICA", | ||
| "ASIA", | ||
| "EUROPE", | ||
| "LATIN AMERICA AND THE CARIBBEAN", | ||
| "NORTHERN AMERICA", | ||
| "OCEANIA", | ||
| ], | ||
| }, | ||
| Year: year, | ||
| Age: age, | ||
| }, | ||
| }, | ||
| { | ||
| $addFields: { | ||
| TotalPopulation: { | ||
| $sum: { | ||
| $add: ["$M", "$F"], | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ]; //end pipeline | ||
|
|
||
| return await mongoClient.aggregate(pipeline).toArray(); | ||
| }; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "name": "ex1-aggregation", | ||
| "version": "1.0.0", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "description": "", | ||
| "dependencies": { | ||
| "dotenv": "^17.2.3", | ||
| "mongodb": "^7.0.0" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { setup } from "./setup.js"; | ||
| import { transfer } from "./transfer.js"; | ||
|
|
||
| async function main() { | ||
| await setup(); | ||
| await transfer(101, 102, 1000, "Transfer 1000 from 101 to 102"); | ||
| } | ||
|
|
||
| main().catch(console.error); |
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.
You don't need to change this now - just bear this in mind!
Something you may want to consider when you're working with CSV data: Spreadsheets often use Title Case for their headings (
Country,Year, etc.). When we're designing databses, we don't usually use this case. We usually use lowercase or snake_case, removing any whitespace and standardising the cases of the letters.When converting from CSV to database columns you might want to consider your naming conventions. But you do not need to do that for this exercise - jsut something to remember for the future as not following naming conventions will make it harder/less intuitive for other people to work with your databases.