Skip to content
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
102 changes: 102 additions & 0 deletions Week4/homework/ex1-aggregation/index.js
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,

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.

},
},
{
$group: {
_id: "$Year",

Choose a reason for hiding this comment

The 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:

Find a way to get the data in the csv file into your MongoDB database.

Copy link
Author

Choose a reason for hiding this comment

The 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();
};
179 changes: 179 additions & 0 deletions Week4/homework/ex1-aggregation/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Week4/homework/ex1-aggregation/package.json
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"
}
}
9 changes: 9 additions & 0 deletions Week4/homework/ex2-transactions/index.js
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);
Loading