WebOntwikkeling 2024
DigitAP
  • Introductie
  • Tooling
    • Devcontainers
    • Git
    • Terminal en bash
  • NodeJS + TypeScript
    • Wat is NodeJS?
    • Waarom TypeScript?
    • Nieuw project
    • Input lezen
    • Type Systeem
      • Basic types
      • Arrays
      • Interfaces
      • Functions
    • Exceptions
    • Modules
    • Asynchroon Programmeren
      • Promises
      • Async/Await
      • Fetch
      • Extra voorbeelden
  • Express.js
    • Wat is een backend framework?
    • Basis
    • Nodemon
    • Statische Bestanden
    • EJS
    • Request
      • GET Request
      • POST Request
      • Multi-part form data
    • Response
    • Router
    • Middleware
    • Deployment
  • MongoDB
    • Wat is MongoDB?
    • MongoDB driver
    • Insert
    • Find
    • Limit & Sort
    • Query Operators
    • Update
    • Delete
    • Text Search
    • Gebruik in Express.js
      • CRUD
  • Security & Testing
    • Environment Variables
    • Cookies
    • Hashing & Encryptie
    • Sessions
      • Session Based Login
    • JWT Tokens
      • JWT NPM package
      • Tokens veilig opslaan
      • Token Based Login
    • Testing
  • Labos
    • 1. Tooling
    • 2. Basis
      • Hello Name
      • BMI Calculator
      • BMI Calculator Multi
      • Interest Calculator
      • Uren en Minuten
      • Wisselgeld
      • Name from Email
      • Text Box
    • 3. Arrays
      • Som van getallen
      • Puntenboek
      • Rot13
      • Pokemon Team
      • Todo List String
      • Tick Tac Toe
    • 4. Interfaces & functies
      • Interfaces
        • Recepten
        • Movies Objects
        • Todo List Objects
      • Functies
        • Math Fun
        • Short Notation
        • Array Sum
        • Movies Functions
        • Filter Numbers
        • At Least Two
    • 5. Async & fetch
      • Fake Fetch
      • Promise All
      • Slow Sum
      • Todo List Fetch
      • Cocktails Promise All
      • Bitcoin API
      • Joke API
      • School API
      • Cocktails API
      • Unix Timestamp API
    • 6. Modules en NPM Packages
      • Math Module
      • Export Interfaces
      • Cowsay Module
      • Cat GPT
      • Rainbow Chalk
    • 7. Express
      • Hello Express
      • DadJoke Express
      • Bitcoin current
    • 8. EJS + Static
      • Hello Express EJS
      • Cat Static
      • Maaltafels
      • Newspaper
    • 9. Get Request
      • Hello Query
      • Math Service
      • Newspaper Route
      • Newspaper Search
      • Steam
    • 10. Post Request
      • Pet Shelter
      • Redirect Form
      • Contact Form
    • 11. Router en Middleware
      • Router Combi
      • Utility Middleware
      • Rate Limiter
    • 12. Herhaling
      • Twitter
      • Word Guess
    • 13. Mongo Basics
      • Guestbook
      • MoviesDB
      • Pokemon Team
    • 14. Mongo Queries
      • Pet Shelter
      • Games DB
    • 15. Mongo + Express
      • Pet Shelter Express
      • Guestbook Express
      • Pokemon Team Express
    • 16. CRUD
      • Users CRUD
      • Pokedex
    • 17. Session & Cookies
      • Shopping Cookie
      • View Counter Cookies
      • View Counter Sessions
    • 18. Session based login
      • Login Express
      • Pokedex Express Sessions
    • 19. Token based login
      • JWT Sign
      • Login Express JWT
    • 20. Testing
      • Math Test
      • String Test
      • Math Express Test
      • Form Express Test
      • Pet Shelter Express Test
    • 21. Herhaling
      • Youtube Favorites
      • Beerster
    • Extra oefeningen
      • DNA Match
      • View Counter
      • Hogwarts Express CRUD
  • Project
    • Voorbereiding (devcontainer + github)
    • Semester 2
      • Milestone 1 - Terminal App
      • Milestone 2 - Express
      • Milestone 3 - MongoDB
      • Milestone 4 - Security
Powered by GitBook
On this page
  • Functionaliteit
  • Voorbeeld interactie
Export as PDF
  1. Labos
  2. 14. Mongo Queries

Pet Shelter

Maak een nieuw project aan met de naam movies-db en installeer de mongodb package.

We beginnen met de volgende array:

const pets : Pet[] = [
    { name: "Buddy", age: 2, type: "dog", breed: "Golden Retriever" },
    { name: "Daisy", age: 3, type: "dog", breed: "Beagle" },
    { name: "Coco", age: 1, type: "dog", breed: "Poodle" },
    { name: "Charlie", age: 2, type: "cat", breed: "Siamese" },
    { name: "Luna", age: 3, type: "cat", breed: "Persian" },
    { name: "Lucy", age: 1, type: "cat", breed: "Maine Coon" },
    { name: "Max", age: 4, type: "dog", breed: "Labrador Retriever" },
    { name: "Bella", age: 2, type: "dog", breed: "French Bulldog" },
    { name: "Milo", age: 1, type: "dog", breed: "Border Collie" },
    { name: "Oliver", age: 3, type: "cat", breed: "Bengal" },
    { name: "Tiger", age: 2, type: "cat", breed: "Ragdoll" },
    { name: "Zoe", age: 3, type: "cat", breed: "Sphynx" },
    { name: "Sophie", age: 5, type: "dog", breed: "Dachshund" },
    { name: "Lily", age: 1, type: "cat", breed: "British Shorthair" },
    { name: "Oscar", age: 4, type: "dog", breed: "Boxer" },
    { name: "Ruby", age: 2, type: "dog", breed: "Siberian Husky" },
    { name: "Rosie", age: 2, type: "cat", breed: "Scottish Fold" },
    { name: "Jack", age: 3, type: "dog", breed: "Cocker Spaniel" },
    { name: "Sadie", age: 2, type: "dog", breed: "Rottweiler" },
    { name: "Maggie", age: 1, type: "dog", breed: "Shih Tzu" }
];

Functionaliteit

Zorg ervoor dat de applicatie de volgende queries uitvoert:

  • Verwijder alle documenten uit de collection pets

  • Voeg alle pets (de bovenstaande array) toe aan de collection pets

  • Zoek alle pets in de collection van het type "dog" en toon deze in de console

  • Zoek alle pets in de collection van het type "dog" of "cat" en toon deze in de console. Gebruik hiervoor de $in operator.

  • Zoek alle pets in de collection van het type "dog" of "cat" en toon deze in de console. Gebruik hiervoor de $or operator.

  • Zoek alle pets in de collection die tussen 2 en 4 jaar oud zijn en toon deze in de console. Gebruik hiervoor de $gte en $lte operators.

  • Zoek alle pets in de collection die "Retriever" in hun ras hebben en toon deze in de console. Gebruik hiervoor de $text operator.

  • Sorteer alle pets op leeftijd in oplopende volgorde en toon deze in de console.

  • Sorteer alle pets op naam in aflopende volgorde en toon deze in de console.

  • Sorteer alle pets op leeftijd in aflopende volgorde en toon enkel de oudste pet in de console.

  • Sorteer alle pets op leeftijd in oplopende volgorde, sla de eerste 5 pets over en toon enkel de 6de tot 10de jongste pets in de console.

Voorbeeld interactie

All pets in the collection of the type 'dog': 
Buddy     dog       Golden Retriever    2                   
Daisy     dog       Beagle              3                   
Coco      dog       Poodle              1                   
Max       dog       Labrador Retriever  4                   
Bella     dog       French Bulldog      2                   
Milo      dog       Border Collie       1                   
Sophie    dog       Dachshund           5                   
Oscar     dog       Boxer               4                   
Ruby      dog       Siberian Husky      2                   
Jack      dog       Cocker Spaniel      3                   
Sadie     dog       Rottweiler          2                   
Maggie    dog       Shih Tzu            1                   
All pets in the collection of the type 'dog' or 'cat': 
Buddy     dog       Golden Retriever    2                   
Daisy     dog       Beagle              3                   
Coco      dog       Poodle              1                   
Charlie   cat       Siamese             2                   
Luna      cat       Persian             3                   
Lucy      cat       Maine Coon          1                   
Max       dog       Labrador Retriever  4                   
Bella     dog       French Bulldog      2                   
Milo      dog       Border Collie       1                   
Oliver    cat       Bengal              3                   
Tiger     cat       Ragdoll             2                   
Zoe       cat       Sphynx              3                   
Sophie    dog       Dachshund           5                   
Lily      cat       British Shorthair   1                   
Oscar     dog       Boxer               4                   
Ruby      dog       Siberian Husky      2                   
Rosie     cat       Scottish Fold       2                   
Jack      dog       Cocker Spaniel      3                   
Sadie     dog       Rottweiler          2                   
Maggie    dog       Shih Tzu            1                   
All pets in the collection of the type 'dog' or 'cat': 
Buddy     dog       Golden Retriever    2                   
Daisy     dog       Beagle              3                   
Coco      dog       Poodle              1                   
Charlie   cat       Siamese             2                   
Luna      cat       Persian             3                   
Lucy      cat       Maine Coon          1                   
Max       dog       Labrador Retriever  4                   
Bella     dog       French Bulldog      2                   
Milo      dog       Border Collie       1                   
Oliver    cat       Bengal              3                   
Tiger     cat       Ragdoll             2                   
Zoe       cat       Sphynx              3                   
Sophie    dog       Dachshund           5                   
Lily      cat       British Shorthair   1                   
Oscar     dog       Boxer               4                   
Ruby      dog       Siberian Husky      2                   
Rosie     cat       Scottish Fold       2                   
Jack      dog       Cocker Spaniel      3                   
Sadie     dog       Rottweiler          2                   
Maggie    dog       Shih Tzu            1                   
All pets in the collection that are between 2 and 4 years old: 
Buddy     dog       Golden Retriever    2                   
Daisy     dog       Beagle              3                   
Charlie   cat       Siamese             2                   
Luna      cat       Persian             3                   
Max       dog       Labrador Retriever  4                   
Bella     dog       French Bulldog      2                   
Oliver    cat       Bengal              3                   
Tiger     cat       Ragdoll             2                   
Zoe       cat       Sphynx              3                   
Oscar     dog       Boxer               4                   
Ruby      dog       Siberian Husky      2                   
Rosie     cat       Scottish Fold       2                   
Jack      dog       Cocker Spaniel      3                   
Sadie     dog       Rottweiler          2                   
All pets that have 'Retriever' in their breed: 
Max       dog       Labrador Retriever  4                   
Buddy     dog       Golden Retriever    2                   
All pets sorted by age in ascending order: 
Coco      dog       Poodle              1                   
Lucy      cat       Maine Coon          1                   
Milo      dog       Border Collie       1                   
Lily      cat       British Shorthair   1                   
Maggie    dog       Shih Tzu            1                   
Buddy     dog       Golden Retriever    2                   
Charlie   cat       Siamese             2                   
Bella     dog       French Bulldog      2                   
Tiger     cat       Ragdoll             2                   
Ruby      dog       Siberian Husky      2                   
Rosie     cat       Scottish Fold       2                   
Sadie     dog       Rottweiler          2                   
Daisy     dog       Beagle              3                   
Luna      cat       Persian             3                   
Oliver    cat       Bengal              3                   
Zoe       cat       Sphynx              3                   
Jack      dog       Cocker Spaniel      3                   
Max       dog       Labrador Retriever  4                   
Oscar     dog       Boxer               4                   
Sophie    dog       Dachshund           5                   
All pets sorted by name in descending order: 
Zoe       cat       Sphynx              3                   
Tiger     cat       Ragdoll             2                   
Sophie    dog       Dachshund           5                   
Sadie     dog       Rottweiler          2                   
Ruby      dog       Siberian Husky      2                   
Rosie     cat       Scottish Fold       2                   
Oscar     dog       Boxer               4                   
Oliver    cat       Bengal              3                   
Milo      dog       Border Collie       1                   
Max       dog       Labrador Retriever  4                   
Maggie    dog       Shih Tzu            1                   
Luna      cat       Persian             3                   
Lucy      cat       Maine Coon          1                   
Lily      cat       British Shorthair   1                   
Jack      dog       Cocker Spaniel      3                   
Daisy     dog       Beagle              3                   
Coco      dog       Poodle              1                   
Charlie   cat       Siamese             2                   
Buddy     dog       Golden Retriever    2                   
Bella     dog       French Bulldog      2                   
The oldest pet in the collection: 
Sophie    dog       Dachshund           5                   
The 6th to 10th youngest pets in the collection: 
Charlie   cat       Siamese             2                   
Bella     dog       French Bulldog      2                   
Rosie     cat       Scottish Fold       2                   
Ruby      dog       Siberian Husky      2                   
Tiger     cat       Ragdoll             2   
Previous14. Mongo QueriesNextGames DB

Last updated 1 year ago