22. Problem: MongoDB CRUD Operations
Problem Statement: Implement a set of CRUD (Create, Read, Update, Delete) operations for a "Product" entity using MongoDB and Mongoose. Define a Mongoose schema for the product with properties like "name," "price," and "quantity." Implement functions to create, read, update, and delete products.
Function Signature:
/**
* Creates a new product in MongoDB
* @param {Object} product - Product object with properties name, price, and quantity
*/
function createProduct(product) {
// Your implementation here
}
/**
* Retrieves all products from MongoDB
* @returns {Array} - Array of product objects
*/
function getAllProducts() {
// Your implementation here
}
/**
* Updates a product in MongoDB
* @param {string} productId - ID of the product to update
* @param {Object} updatedProduct - Updated product object
*/
function updateProduct(productId, updatedProduct) {
// Your implementation here
}
/**
* Deletes a product from MongoDB
* @param {string} productId - ID of the product to delete
*/
function deleteProduct(productId) {
// Your implementation here
}
Expected Output:
- The functions should perform the respective CRUD operations on the "Product" collection in MongoDB.
Test Cases:
- Create a product, retrieve all products, update a product, and then delete the product.
hint: To solve this problem, you can follow these steps:
- Define a Mongoose schema for the "Product" entity with properties like "name," "price," and "quantity."
- Create a Mongoose model using the schema.
- Implement the createProduct function to create a new product in MongoDB.
- Implement the getAllProducts function to retrieve all products from MongoDB.
- Implement the updateProduct function to update a product in MongoDB.
- Implement the deleteProduct function to delete a product from MongoDB.
- You can use Mongoose methods like save, find, findByIdAndUpdate, and findByIdAndDelete to perform the CRUD operations.