Project 2
Creating Ecommerce in MongoDB
use Ecommerce
db.createCollection("customer")
db.customer.insertMany([
{cust_id:1, cust_name:"Anjali", email:"[email protected]", city:"Delhi", order_value:2000, order_date: "2023-01-23"},
{cust_id:2, cust_name:"Raju", email:"[email protected]", city:"Mumbai", order_value:5000, order_date: "2022-09-23"},
{cust_id:3, cust_name:"Priya", email:"[email protected]", city:"Chennai", order_value:4000, order_date: "2023-01-23"},
{cust_id:4, cust_name:"Vijay", email:"[email protected]", city:"Bangalore", order_value:7000, order_date: "2023-01-23"},
{cust_id:5, cust_name:"Kumar", email:"[email protected]", city:"Bangalore", order_value:10000, order_date: "2023-02-20"},
{cust_id:6, cust_name:"Rohit", email:"[email protected]", city:"Mumbai", order_value:9000, order_date: "2022-10-18"},
{cust_id:7, cust_name:"Selvi", email:"[email protected]", city:"Chennai", order_value:20000, order_date: "2023-01-15"},
{cust_id:8, cust_name:"Ajith", email:"[email protected]", city:"Madurai", order_value:7000, order_date: "2022-11-23"},
{cust_id:9, cust_name:"Karthik", email:"[email protected]", city:"Coimbatore", order_value:15000, order_date: "2023-02-07"},
{cust_id:10, cust_name:"Hari", email:"[email protected]", city:"Trichy", order_value:7500, order_date: "2023-01-23"},
{cust_id:11, cust_name:"Kamal", email:"[email protected]", city:"Bangalore", order_value:1000, order_date: "2023-02-20"},
{cust_id:12, cust_name:"Rajini", email:"[email protected]", city:"Mumbai", order_value:2000, order_date: "2022-10-18"},
{cust_id:13, cust_name:"Selvaraj", email:"[email protected]", city:"Chennai", order_value:30000, order_date: "2023-01-15"},
{cust_id:14, cust_name:"Atlee", email:"[email protected]", city:"Madurai", order_value:4000, order_date: "2022-11-23"},
{cust_id:15, cust_name:"Suriya", email:"[email protected]", city:"Coimbatore", order_value:5000, order_date: "2023-02-07"},
{cust_id:16, cust_name:"Raja", email:"[email protected]", city:"Trichy", order_value:6500, order_date: "2023-01-23"},
])
db.createCollection("products")
db.products.insertMany([
{prod_id:1, prod_name: "Watch", cost:5000, qty:5},
{prod_id:2, prod_name: "Tv", cost:40000, qty:4},
{prod_id:3, prod_name: "Mobile", cost:65000, qty:10},
{prod_id:4, prod_name: "Laptop", cost:80000, qty:9},
{prod_id:5, prod_name: "Headphone", cost:3000, qty:7}
])
db.createCollection("orders")
db.orders.insertMany([
{order_id:1, cust_id: 2, order_status:"delivered", order_date:"2022-09-23"},
{order_id:2, cust_id: 1, order_status:"delivered", order_date:"2023-01-23"},
{order_id:3, cust_id: 3, order_status:"cancelled", order_date:"2023-01-23"}
])
db.orders.insertMany([
{order_id:4, cust_id:4, order_status:"delivered", order_date:"2023-01-23"},
{order_id:5, cust_id:5, order_status:"shipped", order_date:"2023-02-20"},
{order_id:6, cust_id:6, order_status:"delivered", order_date:"2022-10-18"},
{order_id:7, cust_id:7, order_status:"delivered", order_date:"2023-01-15"},
{order_id:8, cust_id:10, order_status:"delivered", order_date:"2023-01-23"},
])
db.createCollection("seller")
db.seller.insertMany([
{seller_id:1, seller_name:"Adani", city:"Bangalore", products_sold:10},
{seller_id:2, seller_name:"Ambani", city:"Mumbai", products_sold:7},
{seller_id:3, seller_name:"Saravana", city:"Chennai", products_sold:9}
])
db.createCollection("categories")
db.categories.insertMany([
{cat_id: 1, cat_name: 'Fashion'},
{cat_id: 2, cat_name: 'Mobiles'},
{cat_id: 3, cat_name: 'Electronics'},
{cat_id: 4, cat_name: 'Appliances'},
])
db.cart.insertMany([
{cart_id: 111},
{cart_id: 222},
{cart_id: 333}
])
db.cart_item.insertMany([
{cart_item_id: 1, prod_id: 1, quantity: 1, total_amount: 5000, cart_id: 111},
{cart_item_id: 2, prod_id: 2, quantity: 1, total_amount: 40000, cart_id: 222},
{cart_item_id: 3, prod_id: 5, quantity: 1, total_amount: 3000, cart_id: 111}
])
db.payment.insertMany([
{payment_id:6001, order_id:1, cust_id:1, payment_date:"2023-01-23", payment_method:"Gpay", total_amount:2000},
{payment_id:6002, order_id:2, cust_id:2, payment_date:"2022-09-23", payment_method:"Debit Card", total_amount:5000},
{payment_id:6003, order_id:3, cust_id:1, payment_date:"2023-02-17", payment_method:"Credit Card", total_amount:6000}
])
Queries:
1. Write a query to find the average order value for all Customers.
Answer:
db.customer.aggregate([
{$group:{_id:"cust_id",average_order_value:{$avg:"$order_value"}}}
])
Output:
2. Query your database to get top 10 customers whose orders have the highest value and who have been customers since less than 2 months.
Answer:
db.customer.aggregate([
{$match:{order_date:{$gt:"2023-01-01", $lte:"2023-02-28"}}},
{$sort:{order_value:-1}},
{$limit:10}
])
Output:




3. Query your database to find the sum of all orders received in this month.
Answer:
db.orders.aggregate([
{$match:{order_date:{$gte:"2023-01-01", $lte:"2023-01-31"}}},
{$group:{_id:"order_id", "sum of all orders":{$sum:1}}}
])
Output:

4. Query your database to find customers count in various cities, group them by names of cities.
Answer:
db.customer.aggregate([
{$group:{_id:"$city", "Number of Customers":{$sum:1}}}
])
Output:

5. Query your database to get all the sellers who have base in the same city in which the customers who ordered their products. Order them in the order of highest number of products sold to customers in same city to lowest.
Answer:
db.seller.aggregate([
{
$lookup:
{
from:"customer",
localField:"city",
foreignField:"city",
as:"Order details"
}
},
{
$sort:
{
products_sold:-1
}
}
])
Output:




