Mongo Aggregate & nodejs

Exercises: The Mongo Client

  • Create a new node project

  • In the terminal run npm install mongodb --save

  • Inspect the following code

    • Insert the boilerplate code in your project

Step 1 in app.js

import { MongoClient } from "mongodb";

const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri);

await client.connect();
const db = client.db("pokemons");
const collection = db.collection("trainers");

async function getSingleTrainer(){
    const aTrainer = await collection.findOne({hometown:"Pallet Town"});
    console.log(aTrainer);
}

Step 2 in package.json: Add "type": module to the JSON object

Step 3: Import the dataset: https://github.com/nicklasdean/data/blob/main/trainers.json to MongoDB (compass)

If we want to find all trainers from "Pallet Town the query looks like this:

Exercises 1

A) Write a function that fetches the first trainer from Pallet Town

  • Name: "Ash Ketchum"

B) Write a function that finds all trainers who are younger than 20

  • Length: 6 [Ash, Misty, Gary, Janine, Ritchie, Tracey]

C) Write a function that finds all trainers with "Pikachu" or "Starmie" in their team.

  • Use projection to only display their names

  • Answer: [Ash, Misty, Ritchie]

**D) ** Write a function that finds all trainers from the "Kanto" region that has a battle style that is offensive

  • 4 [Ash, Lt. Surge, Blaine, Giovanni]

E) Write a function that finds all trainers whose speciality_type is either "Fire" or "Electric"

  • [Lt. Surge, Blaine, Ritchie]

F) Write a function that finds all trainers in the "Indigo League" with more than 25 years of experience

  • [Agatha, Professor Oak]

G) Write a function that prints out how many trainers have "Pikachu" as their favourite Pokemon

  • 2

H) Write a function that takes a trainer name as argument.

  • If the trainer name is found all data about the trainer is logged to the console.

  • If the trainer is not found - log "No such trainer found" to the console.

  • Hint: You can use JavaScript functionality for this

I) Write a function that takes a number as argument

  • Print an array of the trainers name who has that number of badges.

Advanced (Optional)

J) Write a function that calculates how many pokemons the trainers have on average

K) Write a function that counts the number of trainers from each Hometown

  • Hint store the count of trainers in an object and use: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn

Exercises: Aggregation

A) Write a function that calculates the average years of experience of all trainers.

  • Hint: If we want to use average on all entities of the dataset we need to use $group.

  • The _id that should be grouped is null (We are not grouping yet, but just calculating the average)

  • Answer: 11.35

B) Write a function that calculates how many trainers use a certain battle style (Offensive, Defensive etc.)

  • Answer:

C) Write a function that finds the region with most trainers

  • Answer: Kanto: 15 trainers

  • Hint: remember the "limit" and "sort (order by)" step

D) Write a function that finds the top 3 team affiliations with the most years of experience

  • Hint: We can group by one field with and $count, $average etc as such:

**Answer: **

Advanced (Optional)

E) Write a function that finds what specialty type has the highest average years of experience?

  • Ghost: 40

F) Write a function that finds the most common tag among all trainers

  • Hint you can use the $unwind pipeline step to break an array intro individual values

  • Answer: league: 18

Imagine you have documents like this:

Before unwind:

After unwind $unwind:

Last updated