Challenge

Project 2

db.createCollection("customers")

db.customers.insertOne({
"id": 1,
"name": "Sourav Mondal",
"email": "[email protected]",
"password": "password123",
"address": "asansol, west bengal",
"phone": "1234567890",
"type": "normal"
});

db.customers.insertOne({
"id": 2,
"name": "Gourav Mondal",
"email": "[email protected]",
"password": "password456",
"address": "Durgapur, west bengal",
"phone": "9876543210",
"type": "premium"
});


db.createCollection("products")

db.products.insertOne({
"id": 1,
"name": "Product 1",
"description": "Description of Product 1",
"price": 190.99,
"brand": "Brand A",
"segments": ["Segment 1", "Segment 2"],
"categories": ["Category A", "Category B"],
"tags": ["Tag 1", "Tag 2"]
});

db.products.insertOne({
"id": 2,
"name": "Product 2",
"description": "Description of Product 2",
"price": 229.99,
"brand": "Brand B",
"segments": ["Segment 1"],
"categories": ["Category B"],
"tags": ["Tag 2", "Tag 3"]
});


db.createCollection("categories")

db.categories.insertOne({
"id": 1,
"name": "Category A",
"parentCategoryId": null
});

db.categories.insertOne({
"id": 2,
"name": "Category B",
"parentCategoryId": null
});

db.categories.insertOne({
"id": 3,
"name": "Subcategory A",
"parentCategoryId": 1
});

db.createCollection("carts")

db.carts.insertOne({
"id": 1,
"customerId": 1,
"products": [
{
"productId": 1,
"quantity": 2
}
]
});


db.createCollection("orders")

db.orders.insertOne({
"id": 1,
"customerId": 1,
"sellerId": 1,
"products": [
{
"productId": 1,
"quantity": 2
}
]),
"status": "Pending",
"orderDate": ISODate("2023-05-24T12:00:00Z"),
"shippingDetails": {
"address": "asansol, west bengal",
"phone": "1234567890"
},
"billingInformation": {
"name": "Gourav Mondal",
"address": "durgapur, west bengal",
"phone": "9876543210",
"paymentMethod": "Credit Card"
}
);

db.createCollection("sellers")

db.sellers.insertOne({
"_id": 1,
"name": "Seller 1",
"email": "[email protected]",
"password": "seller123",
"address": "nagpur, maharastra",
"phone": "9876543210"
});

Simple Queries to perform on ecommerce db

  1. Write a query to find the average order value for all Customers.

db.orders.aggregate([
{
$group: {
_id: null,
averageOrderValue: { $avg: { $sum: "$products.price" } }
}
}
]);

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.orders.aggregate([
{
$match: {
orderDate: { $gt: ISODate("2023-03-24T00:00:00Z") }
}
},
{
$group: {
_id: "$customerId",
totalOrderValue: { $sum: { $sum: "$products.price" } }
}
},
{
$sort: { totalOrderValue: -1 }
},
{
$limit: 10
}
]);

3.Query your database to find the sum of all orders received in this month

db.orders.aggregate([
{
$match: {
orderDate: {
$gte: ISODate("2023-05-01T00:00:00Z"),
$lt: ISODate("2023-06-01T00:00:00Z")
}
}
},
{
$group: {
_id: null,
totalOrderValue: { $sum: { $sum: "$products.price" } }
}
}
]);

4.Query your database to find customers count in various cities, group them by names of cities

db.customers.aggregate([
{
$group: {
_id: "$address.city",
count: { $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.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.