Project 2
1. Design your ecommerce database in MongoDB with the specifications provided Below :
use EcommerceData
Customers
db.createCollection('Customer')
EcommerceData> db.Customer.insertMany([
{_id:'[email protected]',name:'Vishal Kumar',type:'premium',orderAmount:2000,
Address:{City:'Akola', State:'Maharashtra'}},
{_id:'[email protected]',name:'Seema Patil',type:'premium',orderAmount:9000,
Address:{City:'Nagpur', State:'Maharashtra'}},
{_id:'[email protected]',name:'Sonali Bagde',type:'Normal',orderAmount:6500,
Address:{City:'Nagpur', State:'Telangana'}},
{_id:'[email protected]',name:'Akshay Kumar',type:'Normal',orderAmount:4000,
Address:{City:'Pune', State:'Maharashtra'}}
])
Products
db.product.insertMany([{_id:1,title:'Samsung Mobile', category:'Mobile',price:12000},
... {_id:2,title:'Dell Laptop', category:'laptop',price:40000},
... {_id:3,title:'Apple Mobile', category:'Mobile',price:75000},
... {_id:4,title:'Samsung TV', category:'TV',price:50000},
... {_id:5,title:'Java Programming', category:'Book',price:500}])
The Carts
db.carts.insertMany([
{
status:'active',
quantity: 2,
total: 60000,
products: ['Samsung Moblie','Samsung TV']},
{
status:'active',
quantity: 1,
total: 12000,
products: ['Samsung Moblie']
}
]);
Checkout
db.orders.insertOne({
...
... shipping: {
... customer: "Jay S Londhe",
... address: "Akot Fail",
... city: "City",
... region: "",
... state: "Maharashtra",
... country: "India",
...
...
... tracking: {
... company: "ABC",
... tracking_number: "22122X211SD",
... status: "Deliverd",
...
... },
... },
...
... payment: {
... method: "UPI",
... transaction_id: "2312213312XXXTD"
... },
...
... products: {
... quantity:1, id:1, title:"Samsung mobile", price:12000
... }
... })
Payment
db.payments.insertOne( {
... customerId: "[email protected]",
... status: "verified",
...
... upi:'jay1111@upi'
... } )
Seller
db.seller.insertMany([
... {_id:'[email protected]',
... name:'Vivek Kumar',
... city:'Pune',
... orderReceived:{ title:'Samsung Mobile',
... qty:5,
... price:60000,
... custmor_id:'[email protected]'},
... }])
2. Simple Queries to perform on ecommerce db
1. Write a query to find the average order value for all Customers.
EcommerceData> db.Customer.aggregate([{$group:{ _id:'_id', AverageOrder:{$avg:'$orderAmount'}}}]);
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
EcommerceData> db.Customer.find({},{name:1,orderAmount:1,_id:0}).sort({orderAmount:-1}).limit(10)
3. Query your database to find the sum of all orders received in this month.
EcommerceData> db.Customer.aggregate([{$group:{_id:'_id',sum:{$sum:'$orderAmount'}}}])
4.Query your database to find customers count in various cities, group them by names of cities
EcommerceData> db.Customer.aggregate([{$group:{_id:'$Address.City', No_of_Customer:{$sum:1}}}])
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.
db.seller.aggregate([
... { $lookup:
... {
... from: 'customer',
... localField: 'custmor_id',
... foreignField: '_id',
... as: 'Result'
... },
... $group:{
... _id:"city"
... }
... }
...
...
...
... ])