Assignment-2
1.Design a MongoDB schema for an e-commerce website to store product
information, including products, categories, and orders.
Ans =
1. Products Collection
Stores product details like name, price, category, and stock.
{
"_id": ObjectId("product_id"),
"name": "Laptop",
"description": "High-performance laptop",
"price": 1200,
"category_id": ObjectId("category_id"),
"stock": 50
}
2. Categories Collection
Stores product categories.
{
"_id": ObjectId("category_id"),
"name": "Electronics"
}
3. Orders Collection
Stores customer orders, linking products with quantities.
{
"_id": ObjectId("order_id"),
"customer_name": "John Doe",
"customer_email": "johndoe@example.com",
"order_date": ISODate("2024-02-09T12:00:00Z"),
"items": [
{
"product_id": ObjectId("product_id"),
"quantity": 2,
"price": 1200
}
],
"total_price": 2400,
"status": "Pending"
}
2.Develop a script to connect to MongoDB and perform basic CRUD
operations on a to-do list collection.
Ans =
Python script using PyMongo to connect to MongoDB and perform CRUD operations on
a to-do list collection.
from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["todo_db"]
todo_collection = db["todos"]
# Create: Insert a new task
def add_task(task, status="pending"):
todo_collection.insert_one({"task": task, "status": status})
print(f"Task '{task}' added successfully!")
# Read: Retrieve all tasks
def get_tasks():
tasks = todo_collection.find()
for task in tasks:
print(task)
# Update: Modify task status
def update_task(task, new_status):
todo_collection.update_one({"task": task}, {"$set": {"status": new_status}})
print(f"Task '{task}' updated to '{new_status}'.")
# Delete: Remove a task
def delete_task(task):
todo_collection.delete_one({"task": task})
print(f"Task '{task}' deleted successfully!")
if __name__ == "__main__":
add_task("Buy groceries")
add_task("Complete assignment", "in progress")
print("\nAll Tasks:")
get_tasks()
update_task("Buy groceries", "completed")
print("\nTasks After Update:")
get_tasks()
delete_task("Complete assignment")
print("\nTasks After Deletion:")
get_tasks()
3.Design a MongoDB schema for an e-commerce website to store product
information, including products, categories, and orders.
Ans =
1. Products Collection
Stores product details like name, price, category, and stock.
{
"_id": ObjectId("product_id"),
"name": "Laptop",
"description": "High-performance laptop",
"price": 1200,
"category_id": ObjectId("category_id"),
"stock": 50
}
2. Categories Collection
Stores product categories.
{
"_id": ObjectId("category_id"),
"name": "Electronics"
}
3. Orders Collection
Stores customer orders, linking products with quantities.
{
"_id": ObjectId("order_id"),
"customer_name": "John Doe",
"customer_email": "johndoe@example.com",
"order_date": ISODate("2024-02-09T12:00:00Z"),
"items": [
{
"product_id": ObjectId("product_id"),
"quantity": 2,
"price": 1200
}
],
"total_price": 2400,
"status": "Pending"
}
4.Create indexes in MongoDB to optimize keyword searches for product
names in the e-commerce website database.
Ans =
db.products.createIndex({ "name": "text" })