0% found this document useful (0 votes)
12 views

Logistics Use Case_ Inventory Management System

Uploaded by

devikanair1202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Logistics Use Case_ Inventory Management System

Uploaded by

devikanair1202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Logistics Use Case: Inventory Management System

1. MongoDB Basics and Operations:

1.1 Database and Collection Creation:

● Products (storing product details)


● Suppliers (storing supplier details)
● Orders (storing customer orders)
● Shipments (storing shipment details)

// Create the database


use LogisticsDB;

// Create collections
db.createCollection("Products");
db.createCollection("Suppliers");
db.createCollection("Orders");
db.createCollection("Shipments");

1.2 CRUD Operations:

1.2.1 Create: Insert Documents into Collections:


// Insert a product into the Products collection
db.Products.insertOne({
productId: "P001",
name: "Laptop",
category: "Electronics",
price: 1000,
quantity: 50
});

// Insert a supplier into the Suppliers collection


db.Suppliers.insertOne({
supplierId: "S001",
name: "ABC Electronics",
contact: "John Doe",
phone: "123-456-7890"
});

// Insert an order into the Orders collection


db.Orders.insertOne({
orderId: "O001",
customerName: "Jane Smith",
products: [
{ productId: "P001", quantity: 2 }
],
orderDate: new Date(),
status: "Pending"
});

// Insert a shipment into the Shipments collection


db.Shipments.insertOne({
shipmentId: "SH001",
orderId: "O001",
shipmentDate: new Date(),
shippingAddress: "123 Main St, City, Country",
status: "Shipped"
});
1.2.2 Read: Query Documents:

// Find all products in the 'Products' collection


db.Products.find();

// Find orders for a specific customer


db.Orders.find({ customerName: "Jane Smith" });
// Find a shipment by shipment ID
db.Shipments.findOne({ shipmentId: "SH001" });

1.2.3 Update: Modify Documents:

// Update the quantity of the product 'Laptop' after a sale


db.Products.updateOne(
{ productId: "P001" }, // Filter
{ $inc: { quantity: -2 } } // Decrement quantity by 2
);

1.2.4 Delete: Remove Documents:

// Delete an order that was cancelled


db.Orders.deleteOne({ orderId: "O001" });

1.3 Projection:

// Retrieve only the name and price of products


db.Products.find({}, { name: 1, price: 1 });

// Retrieve order details but exclude the products field


db.Orders.find({ orderId: "O001" }, { products: 0 });

2. MongoDB Query Methods:

2.1 limit() and skip():

// Limit the number of orders returned to 10


db.Orders.find().limit(10);

// Skip the first 10 orders and return the next 10


db.Orders.find().skip(10).limit(10);

2.2 sort():

// Sort orders by orderDate in descending order


db.Orders.find().sort({ orderDate: -1 });

3. Indexes and Performance:

3.1 Indexes:

// Create an index on productId in the Products collection


db.Products.createIndex({ productId: 1 });

// Create an index on orderId in the Orders collection


db.Orders.createIndex({ orderId: 1 });

// Create a compound index on customerName and orderDate in Orders for faster


queries by customer and date
db.Orders.createIndex({ customerName: 1, orderDate: -1 });

3.2 Viewing Indexes:

// View indexes in the Products collection


db.Products.getIndexes();
// View indexes in the Orders collection
db.Orders.getIndexes();

3.3 Dropping Indexes:

// Drop the index on productId in the Products collection


db.Products.dropIndex("productId_1");
// Drop all non-default indexes in the Orders collection
db.Orders.dropIndexes();
1. Greater Than ($gt)

// Find products with a price greater than 500:

db.Products.find({ price: { $gt: 500 } });

2. Less Than ($lt)

// Find products with a price less than 200:

db.Products.find({ price: { $lt: 200 } });

3. Not Equals ($ne)

// Find products that are not in the "Electronics" category:

db.Products.find({ category: { $ne: "Electronics" } });

4. Greater Than or Equal to ($gte)

// Find products with a price greater than or equal to 500:

db.Products.find({ price: { $gte: 500 } });

You might also like