5. Examples

Base repos for all the following examples can be found here

Example 1: CLI-Based Item Manager

You are tasked with building a Command Line Interface (CLI) application that connects to a MongoDB database via Mongoose. The application will manage a simple list of items. Your app should:

  1. Create a table in the database for storing item information if it doesn’t already exist.
  2. Insert a new item into the table by passing the item name as a command-line argument.
  3. Display all items stored in the table.

Functional Requirements:

  • The Item collection in MongoDB should have the following fields:

    • _id: The unique identifier generated automatically by MongoDB.
    • name (String): The name of the item.
  • The CLI should support two commands:

    1. insert <item_name>: Inserts an item with the provided name into the database.
    2. show: Displays all the items currently stored in the collection.

Detailed Breakdown

  1. Setting up the Mongoose connection:

    • Use the mongoose package to connect to your MongoDB database.
    • Define the connection string in your script (e.g., mongodb://localhost:27017/items_db).
  2. Creating the Item model:

    • Define a schema using Mongoose’s Schema class with the required fields.
    • Create a Mongoose model from the schema for interacting with the items collection.
  3. Inserting data:

    • For the insert <item_name> command, create a new Item document using the Mongoose model and save it to the database.
  4. Displaying all data:

    • For the show command, retrieve all documents from the items collection and display them in the CLI.

Input/Output Examples

  • Inserting an item:

    • Command:
      node index.js insert "Apple"
    • Output:
      Inserted item: Apple
  • Displaying all items:

    • Command:
      node index.js show
    • Output:
      Items in the table:
      1: Apple
      2: Orange

Example 2: CLI-Based To-Do List Manager

You are tasked with building a Command Line Interface (CLI) application that connects to a MongoDB database via Mongoose. The application will manage a to-do list. Your app should:

  1. Create a schema and model for storing to-do items.
  2. Insert a new to-do item into the database via command-line arguments.
  3. Display all to-do items stored in the database.
  4. Mark a to-do item as completed by its ID.
  5. Delete a to-do item by its ID.

Functional Requirements:

  • The ToDo collection should have the following fields:

    • _id: The unique identifier generated by MongoDB.
    • task (String): The description of the to-do item.
    • completed (Boolean): Whether the item has been completed (default: false).
  • The CLI should support these commands:

    1. add <task>: Adds a new to-do item with the provided task description.
    2. show: Displays all to-do items.
    3. complete <id>: Marks the to-do item with the given ID as completed.
    4. delete <id>: Deletes the to-do item with the specified ID.

Example 3: Library Book Management System

You are tasked with building a Library Book Management System that connects to a MongoDB database and allows users to manage book data. Your app should be able to:

  1. List all books in the library.
  2. Add a new book with a title, author, and publication year.
  3. Update book details by book ID.
  4. Retrieve book details by book ID.

Functional Requirements:

  • The database should have a collection called books with the following fields:

    • _id: The unique identifier for each book (generated by MongoDB).
    • title (String): The title of the book.
    • author (String): The author of the book.
    • publication_year (Number): The year the book was published.
  • The Express app should support the following endpoints:

    1. GET /books: Returns a list of all books.
    2. POST /books: Adds a new book with the provided title, author, and publication year.
    3. PUT /books/:id: Updates the book details for the specified ID.
    4. GET /books/:id: Retrieves book details for the specified ID.

Detailed Breakdown

  1. Setting up the database connection:

    • Use the mongoose npm package to connect to your MongoDB database.
    • Define a Book schema and model.
  2. Listing all books:

    • Implement the GET /books endpoint that queries the books collection and returns all book documents in JSON format.
  3. Adding a new book:

    • Implement the POST /books endpoint that takes book information from the request body and inserts it into the books collection.
  4. Updating book details:

    • Implement the PUT /books/:id endpoint that updates the book document (title, author, and publication year) based on the provided book ID.
  5. Retrieving book details:

    • Implement the GET /books/:id endpoint that retrieves and returns the book document for a specific book ID.

Example 4: Product Inventory System

You are tasked with building a Product Inventory System that connects to a MongoDB database and allows users to manage product data. Your app should be able to:

  1. List all products stored in the database.
  2. Add a new product with a name, price, and stock quantity.
  3. Update product information by product ID.
  4. Delete a product by product ID.

Functional Requirements:

  • The database should have a collection called products with the following fields:

    • _id: The unique identifier for each product (generated by MongoDB).
    • name (String): The name of the product.
    • price (Number): The price of the product.
    • stock_quantity (Number): The quantity of the product in stock.
  • The Express app should support the following endpoints:

    1. GET /products: Returns a list of all products.
    2. POST /products: Adds a new product with the provided name, price, and stock quantity.
    3. PUT /products/:id: Updates the product information for the specified ID.
    4. DELETE /products/:id: Deletes the product with the specified ID.

Detailed Breakdown

  1. Setting up the database connection:

    • Use the mongoose npm package to connect to your MongoDB database.
    • Define a Product schema and model.
  2. Listing all products:

    • Implement the GET /products endpoint that queries the products collection and returns all product documents in JSON format.
  3. Adding a new product:

    • Implement the POST /products endpoint that takes product information from the request body and inserts it into the products collection.
  4. Updating product information:

    • Implement the PUT /products/:id endpoint that updates the product document (name, price, and stock quantity) based on the provided product ID.
  5. Deleting a product:

    • Implement the DELETE /products/:id endpoint that removes the product document with the specified ID from the products collection.

Example 5: User Management System

You are tasked with building a User Management System that connects to a MongoDB database and allows users to manage user data. Your app should be able to:

  1. List all users stored in the database.
  2. Add a new user with a name and email address.
  3. Update user information by user ID.
  4. Retrieve user information by user ID.

Functional Requirements:

  • The database should have a collection called users with the following fields:

    • _id: The unique identifier for each user (generated by MongoDB).
    • name (String): The name of the user.
    • email (String): The email address of the user.
  • The Express app should support the following endpoints:

    1. GET /users: Returns a list of all users.
    2. POST /users: Adds a new user with the provided name and email.
    3. PUT /users/:id: Updates the user information for the specified ID.
    4. GET /users/:id: Retrieves user information for the specified ID.

Detailed Breakdown

  1. Setting up the database connection:

    • Use the mongoose npm package to connect to your MongoDB database.
    • Define a User schema and model.
  2. Listing all users:

    • Implement the GET /users endpoint that queries the users collection and returns all user documents in JSON format.
  3. Adding a new user:

    • Implement the POST /users endpoint that takes user information from the request body and inserts it into the users collection.
  4. Updating user information:

    • Implement the PUT /users/:id endpoint that updates the user document (name and email) based on the provided user ID.
  5. Retrieving user information:

    • Implement the GET /users/:id endpoint that retrieves and returns the user document for a specific user ID.