Project 2
db.Ecommerce.insertMany([
{admin_id:00},{admin_id:01},{sel_id:02},{sel_id:03},{sel_id:04},{sel_id:05},{sel_id:06},{cust_id:07},{cust_id:08},{cust_id:09},{cust_id:10},{cust_id:11},{cust_id:12},
{cust_id:13},{cust_id:14},{cust_id:15},{cust_id:16},{cust_id:17},{cust_id:18},{cust_id:19},{cust_id:20},])
db.Admin.insertMany([{admin_id:11,admin_name:"SAIRAM",admin_city:"CHENNAI",admin_mobil:"123456789"},{admin_id:12,admin_name:"KUMAR",admin_city:"MADURAI",admin_mobil:"37901232"}])
db.customer.insertMany([
{cust_type:"Premium",cust_id:21,cust_name:"SAIRAM",cust_gender:"M",cust_Doj_as_per_date:"2-1-2022",cust_Doj_as_per_month:"1 Month",cust_city:"CHENNAI",cust_mb_no:"683937292917",order_value:80000},
{cust_type:"Premium",cust_id:22,cust_name:"PRAVEEN",cust_gender:"M",cust_Doj_as_per_date:"12-12-2022",cust_Doj_as_per_month:"1 Month",cust_city:"mpk",cust_mb_no:"64846399272",order_value:4000},
{cust_type:"Premium",cust_id:23,cust_name:"KISHANA",cust_gender:"M",cust_Doj_as_per_date:"11-11-2022",cust_Doj_as_per_month:"1 Month",cust_city:"TVT",cust_mb_no:"05847699202",order_value:30000},
{cust_type:"Normal",cust_id:24,cust_name:"MUSTAN",cust_gender:"M",cust_Doj_as_per_date:"24-10-2022",cust_Doj_as_per_month:"1 Month",cust_city:"TVT",cust_mb_no:"3463289012",order_value:8000},
{cust_type:"Premium",cust_id:25,cust_name:"vasu",cust_gender:"M",cust_Doj_as_per_date:"25-10-2022",cust_Doj_as_per_month:"1 Month",cust_city:"TVT",cust_mb_no:"0584639893",order_value:5000},
{cust_type:"Normal",cust_id:26,cust_name:"kumar",cust_gender:"M",cust_Doj_as_per_date:"30-10-2022",cust_Doj_as_per_month:"1 Month",cust_city:"COIMBUTAR",cust_mb_no:"7418857744",order_value:45000},
{cust_type:"Premium",cust_id:27,cust_name:"arivazhagan",cust_gender:"M",cust_Doj_as_per_date:"30-10-2022",cust_Doj_as_per_month:"1 Month",cust_city:"chennai",cust_mb_no:"9383735372",order_value:45000},
{cust_type:"Normal",cust_id:28,cust_name:"parthi",cust_gender:"M",cust_Doj_as_per_date:"30-08-2022",cust_Doj_as_per_month:"1 Month",cust_city:"THIRUCHAI",cust_mb_no:"8807919181",order_value:50000},
{cust_type:"Premium",cust_id:29,cust_name:"prithiv",cust_gender:"M",cust_Doj_as_per_date:"30-08-2022",cust_Doj_as_per_month:"1 Month",cust_city:"EURODE",cust_mb_no:"0000000000",order_value:4000},
{cust_type:"Premium",cust_id:211,cust_name:"MAMA",cust_gender:"M",cust_Doj_as_per_date:"30-08-2022",cust_Doj_as_per_month:"1 Month",cust_city:"THIRUVOTTIYUR",cust_mb_no:"0000000007",order_value:4000},
{cust_type:"Normal",cust_id:212,cust_name:"NAIDU",cust_gender:"M",cust_Doj_as_per_date:"30-08-2022",cust_Doj_as_per_month:"1 Month",cust_city:"villivakam",cust_mb_no:"9876543210",order_value:4500},
{cust_type:"Premium",cust_id:213,cust_name:"VIGNESH",cust_gender:"M",cust_Doj_as_per_date:"30-08-2022",cust_Doj_as_per_month:"2 Month",cust_city:"PORUR",cust_mb_no:"000000001",order_value:36000},
{cust_type:"Normal",cust_id:214,cust_name:"KUMAR",cust_gender:"M",cust_Doj_as_per_date:"30-08-2022",cust_Doj_as_per_month:"2 Month",cust_city:"THORAIPAKKAM",cust_mb_no:"75647486373",order_value:45000},
{cust_type:"Premium",cust_id:215,cust_name:"vicky",cust_gender:"M",cust_Doj_as_per_date:"30-08-2022",cust_Doj_as_per_month:"1 Month",cust_city:"PERUNGADI",cust_mb_no:"8464638736",order_value:450000}
])
db.seller.insertMany([
{sel_id:31,sel_name:"RAM",sel_city:"THIRUVOTTIYUR",pro_catagory:"furniture",sel_mbl_no:"123456789",products_sold:23},
{sel_id:32,sel_name:"RAM",sel_city:"CHENNAI",pro_catagory:"cloths",sel_mbl_no:"123456789",products_sold:22},
{sel_id:33,sel_name:"RAM",sel_city:"PORUR",pro_catagory:"Electronic",sel_mbl_no:"123456789",products_sold:2},
{sel_id:34,sel_name:"RAM",sel_city:"THORIPAKKAM",pro_catagory:"furniture",sel_mbl_no:"123456789",products_sold:30},
{sel_id:35,sel_name:"RAM",sel_city:"kODAMBAKAM",pro_catagory:"Electronic",sel_mbl_no:"123456789",products_sold:20}])
db.product.insertMany([
{pro_catagory:"TABLE",pro_id:101,pro_name:"Table",pro_cost:450000,sel_id:77},
{pro_catagory:"TABLE",pro_id:102,pro_name:"Stul",pro_cost:380000,sel_id:77},
{pro_catagory:"TABLE",pro_id:103,pro_name:"Chair",pro_cost:39000,sel_id:77},
{pro_catagory:"TABLE",pro_id:104,pro_name:"sofa",pro_cost:44000,sel_id:77},
{pro_catagory:"TABLE",pro_id:105,pro_name:"Bench",pro_cost:53000,sel_id:77},
{pro_catagory:"TABLE",pro_id:106,pro_name:"Dining Table",pro_cost:61000,sel_id:77},
{pro_catagory:"Electronic",pro_id:107,pro_name:"Oppo mobile",pro_cost:16000,sel_id:77},
{pro_catagory:"Electronic",pro_id:109,pro_name:"I_phone",pro_cost:121000,sel_id:77},
{pro_catagory:"Electronic",pro_id:110,pro_name:"Redmi",pro_cost:13000,sel_id:77},
{pro_catagory:"Electronic",pro_id:111,pro_name:"Boss mobile",pro_cost:10000,sel_id:77},
{pro_catagory:"Electronic",pro_id:112,pro_name:"Samsung mobile",pro_cost:1200,sel_id:77},
{pro_catagory:"Electronic",pro_id:113,pro_name:"Pocko",pro_cost:14000,sel_id:77},
{pro_catagory:"cloths",pro_id:114,pro_name:"Shirt",pro_cost:15000,sel_id:33,sel_id:77},
{pro_catagory:"cloths",pro_id:115,pro_name:"Normal Phant",pro_cost:8400,sel_id:77},
{pro_catagory:"cloths",pro_id:116,pro_name:"jeans pant",pro_cost:28000,sel_id:77},
{pro_catagory:"cloths",pro_id:117,pro_name:"Track phant",pro_cost:9900,sel_id:77},
{pro_catagory:"cloths",pro_id:118,pro_name:"T_shirt",pro_cost:8200,sel_id:77},
{pro_catagory:"cloths",pro_id:119,pro_name:"Hoodie",pro_cost:122000,sel_id:77},
{pro_catagory:"cloths",pro_id:120,pro_name:"Hoodie",pro_cost:11000,sel_id:77}
])
db.cart.insertMany([
{cart_id:10001,cust_id:21,pro_id:101,pro_cost:000},
{cart_id:10002,cust_id:22,pro_id:102,pro_cost:000},
{cart_id:10003,cust_id:23,pro_id:105,pro_cost:000},
{cart_id:10004,cust_id:24,pro_id:106,pro_cost:000},
{cart_id:10005,cust_id:25,pro_id:107,pro_cost:000}
])
db.orders.insertMany([
{order_id:7001,order_value:10000,order_date:"20-11-2022",order_month:"This month",order_status:"Packed",pro_id:101,cust_id:21,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7002,order_value:10000,order_date:"20-11-2022",order_month:"This month",order_status:"Delivered",pro_id:101,cust_id:22,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7003,order_value:10000,order_date:"20-11-2022",order_month:"This month",order_status:"Packed",pro_id:101,cust_id:23,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7004,order_value:10000,order_date:"20-11-2022",order_month:"This month",order_status:"Packed",pro_id:101,cust_id:24,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7005,order_value:10000,order_date:"20-11-2022",order_month:"This month",order_status:"Delivered",pro_id:101,cust_id:25,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7006,order_value:20000,order_date:"20-11-2022",order_month:"This month",order_status:"Packed",pro_id:102,cust_id:26,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7007,order_value:20000,order_date:"20-11-2022",order_month:"This month",order_status:"Delivered",pro_id:102,cust_id:27,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7008,order_value:20000,order_date:"21-10-2022",order_month:"1 month",order_status:"Packed",pro_id:102,cust_id:28,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7009,order_value:20000,order_date:"22-10-2022",order_month:"1 month",order_status:"Delivered",pro_id:102,cust_id:29,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7010,order_value:20000,order_date:"22-10-2022",order_month:"1 month",order_status:"Delivered",pro_id:102,cust_id:211,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7011,order_value:20000,order_date:"22-10-2022",order_month:"1 month",order_status:"Delivered",pro_id:102,cust_id:212,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7012,order_value:20000,order_date:"23-10-2022",order_month:"1 month",order_status:"Packed",pro_id:102,cust_id:213,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7013,order_value:2000,order_date:"24-10-2022",order_month:"1 month",order_status:"Delivered",pro_id:101,cust_id:214,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7014,order_value:2000,order_date:"24-10-2022",order_month:"1 month",order_status:"Packed",pro_id:101,cust_id:21,sel_id:3,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7015,order_value:2000,order_date:"25-10-2022",order_month:"1 month",order_status:"Delivered",pro_id:101,cust_id:21,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7016,order_value:2000,order_date:"25-10-2022",order_month:"1 month",order_status:"Delivered",pro_id:101,cust_id:21,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7017,order_value:2000,order_date:"26-10-2022",order_month:"1 month",order_status:"Delivered",pro_id:101,cust_id:21,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7018,order_value:2000,order_date:"26-10-2022",order_month:"1 month",order_status:"Packed",pro_id:101,cust_id:21,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7019,order_value:2000,order_date:"27-10-2022",order_month:"1 month",order_status:"Delivered",pro_id:101,cust_id:21,sel_id:3,delevery_addr:"23 METTUPALAYAM stree",orders:1},
{order_id:7020,order_value:2000,order_date:"27-10-2022",order_month:"1 month",order_status:"Delivered",pro_id:101,cust_id:21,sel_id:31,delevery_addr:"23 METTUPALAYAM stree",orders:1}
])
db.payment.insertOne({pay_id:1,cust_id:21,order_value:20000,pay_status:"Paid",pay_type:"Gpay" "paypal"})
db.shipment.insertOne({shipment_id:10201,deliver_add:"23 METTUPALAYAM chennai",shiping_status:"processed"})
1.write a query to find the average order value for all customers
Ans:
db.orders.aggregate([{$group:{_id: "$cust_id",avgAmount: { $avg: "$order_value" }}}])
2.Query your database to get top 10 customer whose orders have the
highest value and who have been customers since less than 2 months
ANS:
db.customer.find({"cust_Doj_as_per_month":{"$lt":"2 Months"}}).sort({order_value:-1}).limit(10).pretty()
3.Query your database to find the sum of all orders received in this month
ANS:
db.orders.aggregate([{ $match: { order_month:"This month" } }, { $group: { _id:{month:"$order_month"}, This_Month_orders: { $sum: "$orders" } } }])
4.Query you database to find customers count in various cities, group them by name of cities
ANS:
db.customer.aggregate([{ "$group": { _id: {Customer_city:"$cust_city"}, "count": { "$sum": 1 }, "cust_name": { "$push": "cust_name" } } },
{ "$project": { "size": 1, "customer_count_same_city": { "$size": "$cust_name" } } }])
5.Query you 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.
ANS:
db.getCollection('seller').aggregate([{
$lookup:
{
from:'customer',
localField:'sel_city',
foreignField:'cust_city',
as:'customerdetails'
}
},
{
"$sort":{
"products_sold":-1
}},
{ $match : { "customerdetails" : { $ne : []}}},
]);