Lab 11: MongoDB CRUD
Step 1 — Setup: Connect and Create Collection
# Start MongoDB
docker run -d --name mongo-lab mongo:7
sleep 5
docker exec -it mongo-lab mongosh// Select (or create) a database
use shopdb
// MongoDB creates the database and collection
// on first insert — no CREATE TABLE needed
db.products.drop() // clean start if re-runningStep 2 — insertOne and insertMany
// insertOne: single document
let r1 = db.products.insertOne({
name: "Laptop Pro",
category: "Electronics",
price: 1299.99,
stock: 50,
tags: ["laptop", "portable", "premium"],
specs: { ram: "16GB", storage: "512GB SSD", weight: "1.4kg" },
isActive: true
})
print("Inserted ID:", r1.insertedId)Step 3 — findOne and find with Filters
Step 4 — Comparison and Logical Operators
Step 5 — Projection, Sort, Skip, Limit
Step 6 — updateOne and updateMany
Step 7 — deleteOne, deleteMany, and findOneAndUpdate
Step 8 — Capstone: Product Catalog CRUD Application
Summary
Operation
Command
Filter Example
Last updated
