0% found this document useful (0 votes)
12 views6 pages

E 3

The document outlines a series of MongoDB operations to manage an e-commerce database, including inserting products, customers, and orders. It also demonstrates querying for recent orders, aggregating sales data by product category, and updating product stock. Finally, it shows how to clean up customer records based on order history.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

E 3

The document outlines a series of MongoDB operations to manage an e-commerce database, including inserting products, customers, and orders. It also demonstrates querying for recent orders, aggregating sales data by product category, and updating product stock. Finally, it shows how to clean up customer records based on order history.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

btlab9> db.ecom_products.

insertMany([
{ name: "Laptop", price: 1000, category: "Electronics", stock: 20 },
{ name: "Phone", price: 500, category: "Electronics", stock: 30 },
{ name: "Shirt", price: 40, category: "Clothing", stock: 50 },
{ name: "Pants", price: 60, category: "Clothing", stock: 40 },
{ name: "Shoes", price: 90, category: "Footwear", stock: 25 },
{ name: "Watch", price: 150, category: "Accessories", stock: 10 },
{ name: "Tablet", price: 600, category: "Electronics", stock: 15 },
{ name: "Jacket", price: 120, category: "Clothing", stock: 20 },
{ name: "Backpack", price: 80, category: "Accessories", stock: 35 },
{ name: "Socks", price: 10, category: "Clothing", stock: 60 }
]);
{
acknowledged: true,
insertedIds: {
'0': ObjectId('67f923c16afa8ad1c7b7125e'),
'1': ObjectId('67f923c16afa8ad1c7b7125f'),
'2': ObjectId('67f923c16afa8ad1c7b71260'),
'3': ObjectId('67f923c16afa8ad1c7b71261'),
'4': ObjectId('67f923c16afa8ad1c7b71262'),
'5': ObjectId('67f923c16afa8ad1c7b71263'),
'6': ObjectId('67f923c16afa8ad1c7b71264'),
'7': ObjectId('67f923c16afa8ad1c7b71265'),
'8': ObjectId('67f923c16afa8ad1c7b71266'),
'9': ObjectId('67f923c16afa8ad1c7b71267')
}
}
btlab9> db.ecom_customers.insertMany([
{ fullName: "Anna", email: "anna@example.com", address: "123 Street" },
{ fullName: "Brian", email: "brian@example.com", address: "456 Road" },
{ fullName: "Clara", email: "clara@example.com", address: "789 Lane" },
{ fullName: "Dan", email: "dan@example.com", address: "101 Ave" },
{ fullName: "Ella", email: "ella@example.com", address: "202 Blvd" },
{ fullName: "Fred", email: "fred@example.com", address: "303 Court" },
{ fullName: "Gina", email: "gina@example.com", address: "404 St" },
{ fullName: "Harry", email: "harry@example.com", address: "505 Way" },
{ fullName: "Ivy", email: "ivy@example.com", address: "606 Dr" },
{ fullName: "Jake", email: "jake@example.com", address: "707 Loop" }
]);
{
acknowledged: true,
insertedIds: {
'0': ObjectId('67f923cc6afa8ad1c7b71268'),
'1': ObjectId('67f923cc6afa8ad1c7b71269'),
'2': ObjectId('67f923cc6afa8ad1c7b7126a'),
'3': ObjectId('67f923cc6afa8ad1c7b7126b'),
'4': ObjectId('67f923cc6afa8ad1c7b7126c'),
'5': ObjectId('67f923cc6afa8ad1c7b7126d'),
'6': ObjectId('67f923cc6afa8ad1c7b7126e'),
'7': ObjectId('67f923cc6afa8ad1c7b7126f'),
'8': ObjectId('67f923cc6afa8ad1c7b71270'),
'9': ObjectId('67f923cc6afa8ad1c7b71271')
}
}
btlab9> const products = db.ecom_products.find().toArray();

btlab9> for (let i = 0; i < 20; i++) {


db.ecom_orders.insertOne({
customer_id: ecomCustomers[i % 10]._id,
items: [
{ product_id: products[i % 10]._id, quantity: (i % 3) + 1 }
],
orderDate: new Date(`2025-04-${(i % 10) + 1}`)
});
}
ReferenceError: ecomCustomers is not defined
btlab9> const ecomCustomers = db.ecom_customers.find().toArray();

btlab9> for (let i = 0; i < 20; i++) {


db.ecom_orders.insertOne({
customer_id: ecomCustomers[i % 10]._id,
items: [
{
product_id: products[i % 10]._id,
quantity: (i % 3) + 1
}
],
orderDate: new Date(`2025-04-${(i % 10) + 1}`)
});
}
{
acknowledged: true,
insertedId: ObjectId('67f924916afa8ad1c7b71285')
}
btlab9> db.ecom_orders.find({
orderDate: { $gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) }
});
[
{
_id: ObjectId('67f924916afa8ad1c7b71276'),
customer_id: ObjectId('67f923cc6afa8ad1c7b7126c'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71262'), quantity: 2 }
],
orderDate: ISODate('2025-04-04T17:00:00.000Z')
},
{
_id: ObjectId('67f924916afa8ad1c7b71277'),
customer_id: ObjectId('67f923cc6afa8ad1c7b7126d'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71263'), quantity: 3 }
],
orderDate: ISODate('2025-04-05T17:00:00.000Z')
},
{
_id: ObjectId('67f924916afa8ad1c7b71278'),
customer_id: ObjectId('67f923cc6afa8ad1c7b7126e'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71264'), quantity: 1 }
],
orderDate: ISODate('2025-04-06T17:00:00.000Z')
},
{
_id: ObjectId('67f924916afa8ad1c7b71279'),
customer_id: ObjectId('67f923cc6afa8ad1c7b7126f'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71265'), quantity: 2 }
],
orderDate: ISODate('2025-04-07T17:00:00.000Z')
},
{
_id: ObjectId('67f924916afa8ad1c7b7127a'),
customer_id: ObjectId('67f923cc6afa8ad1c7b71270'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71266'), quantity: 3 }
],
orderDate: ISODate('2025-04-08T17:00:00.000Z')
},
{
_id: ObjectId('67f924916afa8ad1c7b7127b'),
customer_id: ObjectId('67f923cc6afa8ad1c7b71271'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71267'), quantity: 1 }
],
orderDate: ISODate('2025-04-10T00:00:00.000Z')
},
{
_id: ObjectId('67f924916afa8ad1c7b71280'),
customer_id: ObjectId('67f923cc6afa8ad1c7b7126c'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71262'), quantity: 3 }
],
orderDate: ISODate('2025-04-04T17:00:00.000Z')
},
{
_id: ObjectId('67f924916afa8ad1c7b71281'),
customer_id: ObjectId('67f923cc6afa8ad1c7b7126d'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71263'), quantity: 1 }
],
orderDate: ISODate('2025-04-05T17:00:00.000Z')
},
{
_id: ObjectId('67f924916afa8ad1c7b71282'),
customer_id: ObjectId('67f923cc6afa8ad1c7b7126e'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71264'), quantity: 2 }
],
orderDate: ISODate('2025-04-06T17:00:00.000Z')
},
{
_id: ObjectId('67f924916afa8ad1c7b71283'),
customer_id: ObjectId('67f923cc6afa8ad1c7b7126f'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71265'), quantity: 3 }
],
orderDate: ISODate('2025-04-07T17:00:00.000Z')
},
{
_id: ObjectId('67f924916afa8ad1c7b71284'),
customer_id: ObjectId('67f923cc6afa8ad1c7b71270'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71266'), quantity: 1 }
],
orderDate: ISODate('2025-04-08T17:00:00.000Z')
},
{
_id: ObjectId('67f924916afa8ad1c7b71285'),
customer_id: ObjectId('67f923cc6afa8ad1c7b71271'),
items: [
{ product_id: ObjectId('67f923c16afa8ad1c7b71267'), quantity: 2 }
],
orderDate: ISODate('2025-04-10T00:00:00.000Z')
}
]
btlab9> db.ecom_orders.createIndex({ orderDate: 1 });
orderDate_1
btlab9> db.ecom_orders.aggregate([
{ $unwind: "$items" },
{
$group: {
_id: "$items.product_id",
totalSold: { $sum: "$items.quantity" }
}
}
]);
[
{ _id: ObjectId('67f923c16afa8ad1c7b71264'), totalSold: 3 },
{ _id: ObjectId('67f923c16afa8ad1c7b71265'), totalSold: 5 },
{ _id: ObjectId('67f923c16afa8ad1c7b71263'), totalSold: 4 },
{ _id: ObjectId('67f923c16afa8ad1c7b71267'), totalSold: 3 },
{ _id: ObjectId('67f923c16afa8ad1c7b71261'), totalSold: 3 },
{ _id: ObjectId('67f923c16afa8ad1c7b71262'), totalSold: 5 },
{ _id: ObjectId('67f923c16afa8ad1c7b71260'), totalSold: 4 },
{ _id: ObjectId('67f923c16afa8ad1c7b71266'), totalSold: 4 },
{ _id: ObjectId('67f923c16afa8ad1c7b7125e'), totalSold: 3 },
{ _id: ObjectId('67f923c16afa8ad1c7b7125f'), totalSold: 5 }
]
btlab9> db.ecom_orders.aggregate([
{ $unwind: "$items" },
{
$lookup: {
from: "ecom_products",
localField: "items.product_id",
foreignField: "_id",
as: "product"
}
},
{ $unwind: "$product" },
{
$group: {
_id: "$product.category",
revenue: { $sum: { $multiply: ["$items.quantity", "$product.price"] } }
}
}
]);
[
{ _id: 'Electronics', revenue: 7300 },
{ _id: 'Clothing', revenue: 970 },
{ _id: 'Footwear', revenue: 450 },
{ _id: 'Accessories', revenue: 920 }
]
btlab9> db.ecom_orders.aggregate([
{ $unwind: "$items" },
{
$group: {
_id: "$items.product_id",
totalSold: { $sum: "$items.quantity" }
}
},
{ $sort: { totalSold: -1 } },
{ $limit: 5 },
{
$lookup: {
from: "ecom_products",
localField: "_id",
foreignField: "_id",
as: "product"
}
},
{ $unwind: "$product" },
{ $project: { productName: "$product.name", totalSold: 1 } }
]);
[
{
_id: ObjectId('67f923c16afa8ad1c7b7125f'),
totalSold: 5,
productName: 'Phone'
},
{
_id: ObjectId('67f923c16afa8ad1c7b71265'),
totalSold: 5,
productName: 'Jacket'
},
{
_id: ObjectId('67f923c16afa8ad1c7b71262'),
totalSold: 5,
productName: 'Shoes'
},
{
_id: ObjectId('67f923c16afa8ad1c7b71260'),
totalSold: 4,
productName: 'Shirt'
},
{
_id: ObjectId('67f923c16afa8ad1c7b71263'),
totalSold: 4,
productName: 'Watch'
}
]
btlab9> const laptop = db.ecom_products.findOne({ name: "Laptop" });

btlab9> db.ecom_products.updateOne(
{ _id: laptop._id },
{ $inc: { stock: -2 } }
);
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
btlab9> const buyerIds = db.ecom_orders.distinct("customer_id");
btlab9> db.ecom_customers.deleteMany({ _id: { $nin: buyerIds } });
{ acknowledged: true, deletedCount: 0 }
btlab9>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy