Skip to content

BenjaminCreate1/practical

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

//DB

Absolutely, Gaurav! Let’s go step by step with MongoDB CRUD commands for a local MongoDB database (Compass or shell). I’ll give detailed commands, syntax, and examples.


🗄️ MongoDB CRUD Operations (Localhost / Compass / Shell)

Assuming MongoDB is running locally on:

mongodb://127.0.0.1:27017

1️⃣ Connect to MongoDB

Using Mongo Shell:

mongo

or if using a specific database:

mongo --host 127.0.0.1 --port 27017

Switch to database (create if doesn’t exist):

use orderApp

MongoDB creates the database automatically when you insert data.


2️⃣ CREATE — Insert Documents

Syntax

db.collectionName.insertOne({field1: value1, field2: value2, ...})
db.collectionName.insertMany([{...}, {...}, ...])

Example

// Insert a single user
db.users.insertOne({
  name: "Gaurav",
  email: "gaurav@example.com",
  password: "hashed_password"
})

// Insert multiple orders
db.orders.insertMany([
  { product: "Laptop", quantity: 1, price: 50000 },
  { product: "Mouse", quantity: 2, price: 500 }
])

3️⃣ READ — Query Documents

Syntax

db.collectionName.find(query, projection)
db.collectionName.findOne(query)

Examples

// Get all users
db.users.find()

// Get a specific user by email
db.users.findOne({ email: "gaurav@example.com" })

// Get orders with quantity > 1
db.orders.find({ quantity: { $gt: 1 } })

// Only show product and price fields
db.orders.find({}, { product: 1, price: 1, _id: 0 })

4️⃣ UPDATE — Modify Documents

Syntax

db.collectionName.updateOne(filter, update, options)
db.collectionName.updateMany(filter, update, options)
  • $set → update fields
  • $inc → increment numeric fields
  • $unset → remove a field

Examples

// Update a single user's name
db.users.updateOne(
  { email: "gaurav@example.com" },
  { $set: { name: "Gaurav K" } }
)

// Increment order quantity by 1
db.orders.updateOne(
  { product: "Mouse" },
  { $inc: { quantity: 1 } }
)

// Update multiple orders to add a field
db.orders.updateMany(
  {},
  { $set: { status: "pending" } }
)

5️⃣ DELETE — Remove Documents

Syntax

db.collectionName.deleteOne(filter)
db.collectionName.deleteMany(filter)

Examples

// Delete a single order
db.orders.deleteOne({ product: "Mouse" })

// Delete all users with a certain condition
db.users.deleteMany({ email: /@example.com$/ })

6️⃣ ADVANCED — Other Useful Commands

// Count documents
db.orders.countDocuments({ quantity: { $gt: 1 } })

// Sort results
db.orders.find().sort({ price: -1 }) // descending

// Limit results
db.orders.find().limit(3)

// Drop a collection
db.orders.drop()

// Drop the database
db.dropDatabase()

✅ Tips

  1. Always check your database:
show dbs
  1. Check collections in current DB:
show collections
  1. Switch between databases:
use orderApp
  1. Use MongoDB Compass for GUI-based CRUD — same operations but click-and-edit instead of comman

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published