Share
Explore

MongoDB Shell Commands Workbook for Bookstore Inventory & Customer Sales Management

Last edited 157 days ago by System Writer
Learning Outcomes:
This Training workbook for MONGO DB focuses on MONGO DB SHell commands to:
- create a database
CRUD: add,remove, delete and modify data records
Using the theme of a book store: tracking inventory and customer sales

Introduction

Welcome to the MongoDB Shell Commands Workbook for Bookstore Inventory & Customer Sales Management.
This workbook is designed to guide you through the process of
creating a MongoDB database,
adding, removing, deleting, and modifying data records using MongoDB shell commands.
Our theme for this workbook is a bookstore. We will be tracking inventory and customer sales.

Prerequisites

Before we begin, make sure you have the following installed on your system:
MongoDB: Download and install the latest version of MongoDB from .
A text editor: Any text editor like VSCode, Sublime Text, or Atom will work.

Table of Contents

Setting up a MongoDB Database
Inserting Inventory Data
Querying Inventory Data
Updating Inventory Data
Deleting Inventory Data
Inserting Customer Sales Data
Querying Customer Sales Data
Updating Customer Sales Data
Deleting Customer Sales Data
Setting up a MongoDB Database
To start the MongoDB server, open a command prompt or terminal and run the following command:
Study the documentation for mongod here:
npm install mongod
mongod

Open another command prompt or terminal and run the following command to start the MongoDB shell:
Copy code
mongo

To create a database for the bookstore, run the following command in the MongoDB shell:
perlCopy code
use bookstore

Inserting Inventory Data
First, let's create a collection called 'inventory'. To insert a new book into the inventory, use the following command:
db.inventory.insertOne({
title: "The Catcher in the Rye",
author: "J.D. Salinger",
isbn: "9780316769174",
price: 9.99,
stock: 10
});

Querying Inventory Data
To query all books in the inventory, use the following command:
luaCopy code
db.inventory.find();

To find a specific book by ISBN, use the following command (replace '9780316769174' with the ISBN you want to search for):
cssCopy code
db.inventory.findOne({isbn: "9780316769174"});

Updating Inventory Data
To update the stock of a book in the inventory, use the following command (replace '9780316769174' with the ISBN of the book you want to update, and '5' with the new stock value):
cssCopy code
db.inventory.updateOne({isbn: "9780316769174"}, {$set: {stock: 5}});

Deleting Inventory Data
To remove a book from the inventory, use the following command (replace '9780316769174' with the ISBN of the book you want to remove):
cssCopy code
db.inventory.deleteOne({isbn: "9780316769174"});

Inserting Customer Sales Data
First, create a collection called 'sales'. To insert a new customer sale, use the following command:
yamlCopy code
db.sales.insertOne({
customerId: 1,
purchaseDate: new Date(),
items: [
{
isbn: "9780316769174",
quantity: 1
}
],
totalPrice: 9.99
});

Querying Customer Sales Data
To query all customer sales, use the following command:

db.sales.find();

To find sales by a specific customer, use the following command (replace '1' with the customer ID you want to search for):
cssCopy code
db.sales.find({customerId: 1});

Updating Customer Sales Data

Updating Customer Sales Data
To update a customer's sale record, first, find the sale by its _id. For example, if the _id is ObjectId("1234567890abcdef12345678"), use the following command to update the totalPrice to a new value (replace '20.99' with the new total price value):

db.sales.updateOne({_id: ObjectId("1234567890abcdef12345678")}, {$set: {totalPrice: 20.99}});

To update the quantity of a book in a customer's sale record, use the following command (replace '9780316769174' with the ISBN of the book you want to update, and '2' with the new quantity value):

db.sales.updateOne(
{"items.isbn": "9780316769174"},
{$set: {"items.$.quantity": 2}}
);

Deleting Customer Sales Data
To remove a customer's sale record, first, find the sale by its _id. For example, if the _id is ObjectId("1234567890abcdef12345678"), use the following command to remove the sale:

db.sales.deleteOne({_id: ObjectId("1234567890abcdef12345678")});

Conclusion

Congratulations! You have successfully completed the MongoDB Shell Commands Workbook for Bookstore Inventory & Customer Sales Management. You now have a solid understanding of how to create a MongoDB database, add, remove, delete, and modify data records using MongoDB shell commands. You can now use these skills to manage inventory and customer sales data in a real-world bookstore application.

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.