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):
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: newDate(),
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):
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:
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.