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:
- Create a table in the database for storing item information if it doesn’t already exist.
- Insert a new item into the table by passing the item name as a command-line argument.
- Display all items stored in the table.
Functional Requirements:
The
Itemcollection 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:
insert <item_name>: Inserts an item with the provided name into the database.show: Displays all the items currently stored in the collection.
Detailed Breakdown
Setting up the Mongoose connection:
- Use the
mongoosepackage to connect to your MongoDB database. - Define the connection string in your script (e.g.,
mongodb://localhost:27017/items_db).
- Use the
Creating the
Itemmodel:- Define a schema using Mongoose’s
Schemaclass with the required fields. - Create a Mongoose model from the schema for interacting with the
itemscollection.
- Define a schema using Mongoose’s
Inserting data:
- For the
insert <item_name>command, create a newItemdocument using the Mongoose model and save it to the database.
- For the
Displaying all data:
- For the
showcommand, retrieve all documents from theitemscollection and display them in the CLI.
- For the
Input/Output Examples
Inserting an item:
- Command:
node index.js insert "Apple" - Output:
Inserted item: Apple
- Command:
Displaying all items:
- Command:
node index.js show - Output:
Items in the table: 1: Apple 2: Orange
- Command:
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:
- Create a schema and model for storing to-do items.
- Insert a new to-do item into the database via command-line arguments.
- Display all to-do items stored in the database.
- Mark a to-do item as completed by its ID.
- Delete a to-do item by its ID.
Functional Requirements:
The
ToDocollection 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:
add <task>: Adds a new to-do item with the provided task description.show: Displays all to-do items.complete <id>: Marks the to-do item with the given ID as completed.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:
- List all books in the library.
- Add a new book with a title, author, and publication year.
- Update book details by book ID.
- Retrieve book details by book ID.
Functional Requirements:
The database should have a collection called
bookswith 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:
GET /books: Returns a list of all books.POST /books: Adds a new book with the provided title, author, and publication year.PUT /books/:id: Updates the book details for the specified ID.GET /books/:id: Retrieves book details for the specified ID.
Detailed Breakdown
Setting up the database connection:
- Use the
mongoosenpm package to connect to your MongoDB database. - Define a
Bookschema and model.
- Use the
Listing all books:
- Implement the
GET /booksendpoint that queries thebookscollection and returns all book documents in JSON format.
- Implement the
Adding a new book:
- Implement the
POST /booksendpoint that takes book information from the request body and inserts it into thebookscollection.
- Implement the
Updating book details:
- Implement the
PUT /books/:idendpoint that updates the book document (title, author, and publication year) based on the provided book ID.
- Implement the
Retrieving book details:
- Implement the
GET /books/:idendpoint that retrieves and returns the book document for a specific book ID.
- Implement the
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:
- List all products stored in the database.
- Add a new product with a name, price, and stock quantity.
- Update product information by product ID.
- Delete a product by product ID.
Functional Requirements:
The database should have a collection called
productswith 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:
GET /products: Returns a list of all products.POST /products: Adds a new product with the provided name, price, and stock quantity.PUT /products/:id: Updates the product information for the specified ID.DELETE /products/:id: Deletes the product with the specified ID.
Detailed Breakdown
Setting up the database connection:
- Use the
mongoosenpm package to connect to your MongoDB database. - Define a
Productschema and model.
- Use the
Listing all products:
- Implement the
GET /productsendpoint that queries theproductscollection and returns all product documents in JSON format.
- Implement the
Adding a new product:
- Implement the
POST /productsendpoint that takes product information from the request body and inserts it into theproductscollection.
- Implement the
Updating product information:
- Implement the
PUT /products/:idendpoint that updates the product document (name, price, and stock quantity) based on the provided product ID.
- Implement the
Deleting a product:
- Implement the
DELETE /products/:idendpoint that removes the product document with the specified ID from theproductscollection.
- Implement the
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:
- List all users stored in the database.
- Add a new user with a name and email address.
- Update user information by user ID.
- Retrieve user information by user ID.
Functional Requirements:
The database should have a collection called
userswith 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:
GET /users: Returns a list of all users.POST /users: Adds a new user with the provided name and email.PUT /users/:id: Updates the user information for the specified ID.GET /users/:id: Retrieves user information for the specified ID.
Detailed Breakdown
Setting up the database connection:
- Use the
mongoosenpm package to connect to your MongoDB database. - Define a
Userschema and model.
- Use the
Listing all users:
- Implement the
GET /usersendpoint that queries theuserscollection and returns all user documents in JSON format.
- Implement the
Adding a new user:
- Implement the
POST /usersendpoint that takes user information from the request body and inserts it into theuserscollection.
- Implement the
Updating user information:
- Implement the
PUT /users/:idendpoint that updates the user document (name and email) based on the provided user ID.
- Implement the
Retrieving user information:
- Implement the
GET /users/:idendpoint that retrieves and returns the user document for a specific user ID.
- Implement the