Project 2
1. Design your ecommerce database in MongoDB with the specifications provided Below :
MongoDB is a Nosql database collection that focuses on availability and scalability. The data is structured and stored in collections of JSON documents. By not using a fixed data structure, MongoDB provides a comprehensive solution for high-volume storage in a modern distributed system.
- Create your collections for your ecommerce database in MongoDB and fill it with sample information
- db.createcollection customers
- db.createcollection products
- db.createcollection categories
- db.createcollection brands
- db.createcollection segments
- db.createcollection tags
- db.createcollection orders
- db.createcollection sellers
2. Submit all the queries used to create the database
Customers Collection:
db.customers.insert {
"_id": ObjectId("customer_id"),
"name": "John Doe",
"email": "[email protected]",
"password": "password123",
"address": "123 Main St, City",
"phone": "123-456-7890",
"membershipType": "premium"
}
Products Collction:
db.products.insert { "_id": ObjectId("product_id"),
"name": "Product Name",
"price": 9.99,
"description": "Product description",
"categories": ["Electronics", "Computers"],
"brands": ["Brand A"],
"segments": ["Segment A"],
"tags": ["Tag A", "Tag B"]
}
Categories Collection:
db.categories.insert {
"_id": ObjectId("category_id"),
"name": "Electronics"
}
Brands Collection:
db.brands.insert {
"_id": ObjectId("brand_id"),
"name": "Brand A"
}
Segments collection:
db.segments.insert {
"_id": ObjectId("segment_id"),
"name": "Segment A"
}
Tags collection:
db.tags.inserts{
"_id": ObjectId("tag_id"),
"name": "Tag A"
}
Orders Collection :
db.orders.insert {
"_id": ObjectId("order_id"),
"customerId": ObjectId("customer_id"),
"products": [
{
"productId": ObjectId("product_id"),
"quantity": 2
}
],
"status": "Pending",
"shippingDetails": {
"address": "123 Main St, City",
"phone": "123-456-7890"
},
"billingInformation": {
"cardNumber": "**** **** **** 1234",
"expirationDate": "12/24",
"cvv": "***"
}
}
Sellers Collection :
db.sellers.insertOne {
"_id": ObjectId("seller_id"),
"name": "Seller Name",
"email": "[email protected]",
"password": "password123",
"address": "456 Market St, City",
"phone": "987-654-3210"
}
Simple Queries to perform on ecommerce db
1. Write a query to find the average order value for all Customers.
db.customers.find().aggregate(name).sort();
db.customers.find().sort{name:1}).limit(5);
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.
db.customer.find({months:{$lt:10}};
db.checkdate.find( { status: "delivery",estimated_delivery: new ISODate("2012-05-17T08:14:15.656Z") });
3.Query your database to find the sum of all orders received in this month
db.orders.find({$groupby:[{$max:"orders"},{"customer_date"<60}]});
4.Query your database to find customers count in various cities, group them by names of cities
db.aggregatecustomers.insertMany([{titles:'cities',description:'names',by:'count',tags:['cities','names','count'],likes:40}]);
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.customers.aggregate([{$group:[_id{products:"$products"},count: {$sum:1}}}, {$match:{count:{"$lt":1}}} ]);