Yandex

MongoDB - Getting StartedMongoDB - Getting Started1

Project 3: Inventory System (Console-based App)



Project 3: Inventory System (Console-based App)

This mini-project demonstrates how to build a basic Inventory Management System using MongoDB via the shell. You'll simulate real-life inventory operations such as adding products, updating stock, viewing details, and querying products based on conditions.

Project Overview

  • Add new items to inventory
  • View all items
  • Search by category or price
  • Update item stock
  • Remove items from inventory

Real-life scenario: Think of a small electronics store tracking its items. Each product has a name, category, price, quantity, and brand.

Step 1: Create the Database and Collection


    use inventoryDB
    db.createCollection("products")
    { "ok" : 1 }

We now have a database called inventoryDB and a collection called products.

Step 2: Insert Products into Inventory


    db.products.insertMany([
      {
        name: "Wireless Mouse",
        category: "Accessories",
        brand: "Logitech",
        price: 1200,
        quantity: 50
      },
      {
        name: "USB-C Charger",
        category: "Chargers",
        brand: "Anker",
        price: 1500,
        quantity: 30
      },
      {
        name: "Gaming Keyboard",
        category: "Accessories",
        brand: "Corsair",
        price: 3500,
        quantity: 20
      }
    ])
    {
      acknowledged: true,
      insertedIds: { "0": ObjectId("..."), "1": ObjectId("..."), "2": ObjectId("...") }
    }

We’ve now added 3 products with relevant details to our inventory.

Step 3: View All Inventory Items


    db.products.find().pretty()
    {
      "_id" : ObjectId("..."),
      "name" : "Wireless Mouse",
      "category" : "Accessories",
      "brand" : "Logitech",
      "price" : 1200,
      "quantity" : 50
    }
    ...

Question: How can you filter products that are priced below ₹2000?

Answer: You can use the $lt (less than) operator in your query.


    db.products.find({ price: { $lt: 2000 } }).pretty()
    {
      "name": "Wireless Mouse",
      "price": 1200,
      ...
    },
    {
      "name": "USB-C Charger",
      "price": 1500,
      ...
    }

Step 4: Update Stock Quantity

Suppose 5 Gaming Keyboards were sold. Let’s reduce the quantity:


    db.products.updateOne(
      { name: "Gaming Keyboard" },
      { $inc: { quantity: -5 } }
    )
    { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

Explanation: The $inc operator decreases the quantity by 5 units.

Step 5: Find All Products in a Category


    db.products.find({ category: "Accessories" }).pretty()

This helps you list all accessories in the store inventory.

Step 6: Delete an Item

If a product is discontinued or out of stock permanently, you can delete it:


    db.products.deleteOne({ name: "USB-C Charger" })
    { "acknowledged" : true, "deletedCount" : 1 }

Intuition Check: What if you mistakenly deleted an item?

Answer: Unless you have backups or logging, MongoDB won't undo deletes. That's why backups and careful deletion logic are important in production systems.

Wrap Up

You've now built a fully functional console-based inventory system using MongoDB shell. You've learned how to:

  • Insert and structure product documents
  • Query using filters and operators
  • Update stock dynamically
  • Delete items from inventory

This project is a great foundation to expand into a full web-based inventory app using Node.js, Express, and MongoDB.


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M