R
Rishtaara
MongoDB Fundamentals
Lesson 11 of 40Article16 min

CRUD Operations Overview

CRUD stands for Create, Read, Update, Delete — the four basic actions every database application performs. In MongoDB, these map to insert, find, update, and delete operations on documents inside collections.

What is CRUD?

CRUD stands for Create, Read, Update, Delete — the four basic actions every database application performs. In MongoDB, these map to insert, find, update, and delete operations on documents inside collections.

Real-life example: CRUD is like managing a school register — add a new student (Create), look up a name (Read), change a grade (Update), and remove a transferred student (Delete).

Every Rishtaara backend you build — user profiles, product catalogs, order history — boils down to these four operations. Master them in mongosh first, then copy working patterns into Node.js, Python, or any driver.

  • Create — insertOne(), insertMany() add new documents.
  • Read — find(), findOne() retrieve documents with filters.
  • Update — updateOne(), updateMany(), replaceOne() modify existing documents.
  • Delete — deleteOne(), deleteMany() remove documents.
CRUD overview

Create database and collection

MongoDB creates a database the first time you write data to it. Collections are created automatically on the first insert, or you can create them explicitly with db.createCollection().

Real-life example: A database is like a filing cabinet labelled 'rishtaara'. Each drawer inside is a collection — students, products, orders — holding related documents.

MongoDB only shows a database in show dbs after it contains at least one document or collection with data. An empty database is not persisted.
Create database and collection in mongosh
// Switch to (or create) the rishtaara database
use rishtaara

// Explicitly create a collection (optional — insert also creates it)
db.createCollection("students")

// Insert creates the collection if it does not exist
db.products.insertOne({
  name: "Wireless Mouse",
  price: 899,
  category: "electronics",
  stock: 120
})

// Verify
show dbs          // rishtaara appears after first write
show collections  // students, products
Create database using MongoDB Compass
// In Compass GUI:
// 1. Click "+" next to "Databases" → enter "rishtaara" → Create Database
// 2. Enter collection name "students" → Create Collection
// 3. Click "ADD DATA" → "Insert Document" → paste JSON:

{
  "name": "Asha Khan",
  "grade": 10,
  "subjects": ["Mathematics", "Computer Science"],
  "active": true
}

// Compass runs the same insertOne under the hood.

Drop database and drop collection

db.dropDatabase() permanently removes the current database and all its collections. db.collection.drop() removes a single collection and its indexes.

Real-life example: Dropping a collection is like shredding one drawer of files. Dropping a database is like demolishing the entire filing cabinet — every drawer goes.

  • drop() removes the collection and all indexes on it.
  • dropDatabase() removes every collection in the current db.
  • On Atlas, use point-in-time restore if you drop something by mistake.
  • In development, dropping and re-seeding is common — never in production without backups.
Drop collection and database in mongosh
use rishtaara

// Drop a single collection (and its indexes)
db.temp_logs.drop()
// Returns true if collection existed, false otherwise

// Drop the entire current database
db.dropDatabase()
// Returns: { ok: 1, dropped: "rishtaara" }

// Switch back — use recreates on next write
use rishtaara
db.students.insertOne({ name: "Test", grade: 1 })
Drop in MongoDB Compass
// Drop a collection:
// Right-click collection name → Drop Collection → confirm

// Drop a database:
// Right-click database name → Drop Database → type database name → confirm

// ⚠️ There is no undo. Always backup production data before dropping.

CRUD at a glance — quick reference

The legacy methods insert(), update(), remove(), and findAndModify() still exist in older tutorials but are deprecated. Always prefer the explicit One/Many variants — they make intent clear and return useful metadata.

Real-life example: insertOne vs insertMany is like mailing one letter vs a bulk post — same post office, different batch size.

Practice every CRUD command in mongosh before writing driver code. Rishtaara's MongoDB MCQ at /mcq/mongodb-mcq tests these exact patterns.
Modern CRUD one-liners
use rishtaara

// CREATE
db.students.insertOne({ name: "Rahul", grade: 12 })
db.students.insertMany([{ name: "Priya", grade: 11 }, { name: "Vikram", grade: 10 }])

// READ
db.students.find({ grade: { $gte: 10 } })
db.students.findOne({ name: "Rahul" })

// UPDATE
db.students.updateOne({ name: "Rahul" }, { $set: { grade: 12, updatedAt: new Date() } })

// DELETE
db.students.deleteOne({ name: "Vikram" })
db.students.deleteMany({ active: false })