Project 2
Collection for Customer:
Ecommerce> db.createCollection("Customers")
{ ok: 1 }
Ecommerce>
Ecommerce> db.Customers.insertMany([{name:"John Dee",email:"[email protected]",password:"pass1",address:"Kafla Bazar,Cuttack",type:"Normal",cart:[],order:[]},{ name: "Jane Doe", email: "[email protected]", password: "password456", address: "5678 Main St, Los Angeles, CA 90001", type: "premium", cart: [], orders: [] }])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("63e4de800e25bda076c7dcec"),
'1': ObjectId("63e4de800e25bda076c7dced")
}
}
Collection for Categories:
Ecommerce> db.createCollection("categories")
{ ok: 1 }
Ecommerce> db.categories.insertMany([{name:"Electronocs"},{subctegory:[{name:"Mobile"},{name:"Laptops" },{name:"TV"}]},{name: "Fashion",
... subcategories: [
... {
... name: "Men's Clothing"
... },
... {
... name: "Women's Clothing"
... },
... {
... name: "Footwear"
... }
... ]
... }
... ])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("63e634290d984789aadf0d8d"),
'1': ObjectId("63e634290d984789aadf0d8e"),
'2': ObjectId("63e634290d984789aadf0d8f")
}
}
Collection for Brands:
Ecommerce> db.createCollection("brands")
{ ok: 1 }
Ecommerce>
Ecommerce> db.brands.insertMany([
... {
... name: "Apple"
... },
... {
... name: "Samsung"
... },
... {
... name: "Nike"
... }
... ]);
{
acknowledged: true,
insertedIds: {
'0': ObjectId("63e6346d0d984789aadf0d90"),
'1': ObjectId("63e6346d0d984789aadf0d91"),
'2': ObjectId("63e6346d0d984789aadf0d92")
}
}
Collection for segments:
Ecommerce> db.createCollection("segments")
{ ok: 1 }
Ecommerce>
Ecommerce> db.segments.insertMany([
... {
... name: "Luxury"
... },
... {
... name: "Budget"
... }
... ]);
{
acknowledged: true,
insertedIds: {
'0': ObjectId("63e634780d984789aadf0d93"),
'1': ObjectId("63e634780d984789aadf0d94")
}
}
Collection for Products:
Ecommerce> db.products.insertMany([ { name: "iPhone X", price: 999.99, description: "Apple iPhone X with 64GB storage", categories: [ "Electronics", "Smartphones" ], brand: "Apple", segment: "Luxury", tags: [ "Smartphone", "Apple", "Luxury" ], stock: 20 }, { name: "Galaxy S21", price: 899.99, description: "Samsung Galaxy S21 with 128GB storage", categories: [ "Electronics", "Smartphones" ], brand: "Samsung", segment: "Budget", tags: [ "Smartphone", "Samsung", "Budget" ], stock: 30 }, { name: "Air Jordan 1", price: 169.99, description: "Nike Air Jordan 1 sneakers", categories: [ "Fashion", "Footwear"], brand:"Nike", segment:"Luxury", tags:["Fashio","Footwear","Luxury"],stock:25}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("63e635650d984789aadf0d95"),
'1': ObjectId("63e635650d984789aadf0d96"),
'2': ObjectId("63e635650d984789aadf0d97")
}
}
Collection for Cart:
Ecommerce> db.Cart.insertMany([{customer_id:ObjectId("63e4de800e25bda076c7dcec"),items:[{product_id:ObjectId("63e635650d984789aadf0d96"),quantity:3},{product_id:ObjectId("63e635650d984789aadf0d97"),quantity:4}]},{customer_id:ObjectId("63e4de800e25bda076c7dced"),items:[{product_id:ObjectId("63e635650d984789aadf0d96"),quantity:2},{product_id:ObjectId("63e635650d984789aadf0d97"),quantity:1},{product_id:ObjectId("63e635650d984789aadf0d95"),quantity:1}]}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("63e6f36225216a0ef5a553cd"),
'1': ObjectId("63e6f36225216a0ef5a553ce")
}
}
Ecommerce>
Collection for Orders:
Ecommerce> db.orders.insertMany([
... {
... "customer_id": ObjectId("5f22f7c2b0e74b30ccdaf015"),
... "product_id": ObjectId("5f22f7c2b0e74b30ccdaf016"),
... "quantity": 1,
... "total_price": 999.99,
... "billing_info": {
... "name": "John Doe",
... "email": "[email protected]",
... "address": {
... "street": "123 Main St",
... "city": "San Francisco",
... "state": "CA",
... "zip": "94102"
... },
... "payment_method": "Credit Card"
... },
... "shipping_info": {
... "name": "Jane Doe",
... "address": {
... "street": "456 Main St",
... "city": "Los Angeles",
... "state": "CA",
... "zip": "90210"
... },
... "delivery_date": ISODate("2022-12-25T00:00:00.000Z")
... }
... },
... {
... "customer_id": ObjectId("5f22f7c2b0e74b30ccdaf017"),
... "product_id": ObjectId("5f22f7c2b0e74b30ccdaf018"),
... "quantity": 2,
... "total_price": 499.99,
... "billing_info": {
... "name": "Jane Doe",
... "email": "[email protected]",
... "address": {
... "street": "456 Main St",
... "city": "Los Angeles",
... "state": "CA",
... "zip": "90210"
... },
... "payment_method": "Paypal"
... },
... "shipping_info": {
... "name": "John Doe",
... "address": {
... "street": "123 Main St",
... "city": "San Francisco",
... "state": "CA",
... "zip": "94102"
... },
... "delivery_date": ISODate("2022-12-30T00:00:00.000Z")
... }
... }
... ])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("63e707d625216a0ef5a553d1"),
'1': ObjectId("63e707d625216a0ef5a553d2")
}
}
Ecommerce>
To update the customer id by using Bul Write :
Ecommerce>
db.orders.bulkWrite([ { updateOne: { filter: { _id: ObjectId("63e707d625216a0ef5a553d1") }, update: { $set: { "customer_id": ObjectId("63e4de800e25bda076c7dcec") } } } }, { updateOne: { filter: { _id: ObjectId("63e707d625216a0ef5a553d2") }, update: { $set: { "customer_id": ObjectId("63e4de800e25bda076c7dced") } } } } ])
}
Ecommerce> db.orders.find()
[
{
_id: ObjectId("63e707d625216a0ef5a553d1"),
customer_id: ObjectId("63e4de800e25bda076c7dcec"),
product_id: ObjectId("5f22f7c2b0e74b30ccdaf016"),
quantity: 1,
total_price: 999.99,
billing_info: {
name: 'John Doe',
email: '[email protected]',
address: {
street: '123 Main St',
city: 'San Francisco',
state: 'CA',
zip: '94102'
},
payment_method: 'Credit Card'
},
shipping_info: {
name: 'Jane Doe',
address: {
street: '456 Main St',
city: 'Los Angeles',
state: 'CA',
zip: '90210'
},
delivery_date: ISODate("2022-12-25T00:00:00.000Z")
}
},
{
_id: ObjectId("63e707d625216a0ef5a553d2"),
customer_id: ObjectId("63e4de800e25bda076c7dced"),
product_id: ObjectId("5f22f7c2b0e74b30ccdaf018"),
quantity: 2,
total_price: 499.99,
billing_info: {
name: 'Jane Doe',
email: '[email protected]',
address: {
street: '456 Main St',
city: 'Los Angeles',
state: 'CA',
zip: '90210'
},
payment_method: 'Paypal'
},
shipping_info: {
name: 'John Doe',
address: {
street: '123 Main St',
city: 'San Francisco',
state: 'CA',
zip: '94102'
},
delivery_date: ISODate("2022-12-30T00:00:00.000Z")
}
}
]
Collection for seller:
db.createCollection("sellers")
db.sellers.insertMany([
{
"seller_id": ObjectId("5f22f7c2b0e74b30ccdaf015"),
"seller_name": "Seller 1",
"email": "[email protected]",
"password": "$2a$10$oZhE74BHkF.KDPxUOIN.KeOvQZNMHWbP0jgKIRVu22jKHN7ukUcx6",
"address": {
"street": "456 Oak St",
"city": "San Francisco",
"state": "CA",
"zip": "94102"
},
"products": [
{
"product_id": ObjectId("5f22f7c2b0e74b30ccdaf016"),
"product_name": "Product 1",
"category": "Clothing",
"segment": "Men's",
"tags": ["casual", "formal"],
"price": 199.99
},
{
"product_id": ObjectId("5f22f7c2b0e74b30ccdaf017"),
"product_name": "Product 2",
"category": "Electronics",
"segment": "Smartphones",
"tags": ["apple", "samsung"],
"price": 799.99
}
]
},
{
"seller_id": ObjectId("5f22f7c2b0e74b30ccdaf018"),
"seller_name": "Seller 2",
"email": "[email protected]",
"password": "$2a$10$oZhE74BHkF.KDPxUOIN.KeOvQZNMHWbP0jgKIRVu22jKHN7ukUcx6",
"address": {
"street": "789 Pine St",
"city": "San Francisco",
"state": "CA",
"zip": "94102"
},
"products": [
{
"product_id": ObjectId("5f22f7c2b0e74b30ccdaf019"),
"product_name": "Product 3",
"category": "Furniture",
"segment": "Home",
"tags": ["chair", "table"],
"price": 299.99
},
{
"product_id": ObjectId("5f22f7c2b0e74b30ccdaf020"),
"product_name": "Product 4",
"category": "Beauty",
"segment": "Personal Care",
"tags": ["shampoo", "conditioner"],
"price": 199.99
}
]
}
])
Collection for admin:
db.createCollection("admin")
db.admin.insertOne({ "username": "admin", "password": "$2b$10$YJ4/L6xoKPzvyJGQbVlD6uhIjTdxTMcJBl1ddZF9IgukCJzq3Kjnq", "email": "[email protected]" })
-
db.orders.aggregate([{$group:{_id:"$customer_id",avgValue:{$avg:"$total_price"}}}])
-
db.Order.aggregate([ { $match: { "created_at": { $gte: ISODate("2022-12-09T00:00:00.000Z"), $lt: ISODate("2023-02-09T00:00:00.000Z") } } }, { $group: { _id: "$customer_id", total_value: { $sum: "$total_price" } } }, { $sort: { total_value: -1 } }, { $limit: 10 }])
-
Ecommerce> db.orders.aggregate([ { $match: { "created_at": { $gte: new Date(ISODate().getFullYear(), ISODate().getMonth(), 1), $lt: new Date(ISODate().getFullYear(), ISODate().getMonth() + 1, 1) } } }, { $group: { "_id": null, "sum_of_orders": { $sum: "$total_price" } } }]);
-
db.customers.aggregate([ { $group: { _id: "$address.city", count: { $sum: 1 } } }])
-
Ecommerce> db.orders.aggregate([ { $lookup: { from: "sellers", localField: "product_id", foreignField: "_id", as: "seller_info" } }, { $unwind: "$seller_info" }, { $group: { _id: "$seller_info.base_city", sellers: { $addToSet: "$seller_info._id" }, total_quantity: { $sum: "$quantity" } } }, { $sort: { total_quantity: -1 } }, { $project: { _id: 0, base_city: "$_id", sellers: 1, total_quantity: 1 } }])