█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/MongoDB/CRUD Operations
50 minBeginner

CRUD Operations

After this lesson, you will be able to: Use CRUD operations: insertOne/insertMany, find/findOne, updateOne/updateMany, deleteOne/deleteMany.

Mongo's CRUD API is verbose compared to SQL but explicit. Once memorized, it's predictable.

Prerequisites:MongoDB Fundamentals

INSERT operations

Single + bulk inserts.

css
// Single doc
db.users.insertOne({ name: "Alex", email: "[email protected]" });
// → returns { acknowledged: true, insertedId: ObjectId("...") }
// Many docs (atomic per-doc; partial inserts possible if errors)
db.users.insertMany([
{ name: "Sam", email: "[email protected]" },
{ name: "Jordan", email: "[email protected]" }
], { ordered: false }); // ordered:false continues after errors

FIND — read documents

Query filter syntax.

tsx
// All
db.users.find();
// Filter
db.users.find({ age: 30 });
db.users.find({ "address.city": "NYC" }); // nested field
// Projection, only return specific fields (1 = include, 0 = exclude)
db.users.find({ age: 30 }, { name: 1, email: 1, _id: 0 });
// Sort + limit + skip (pagination)
db.users.find().sort({ createdAt: -1 }).limit(10).skip(20);
// findOne, single doc or null
const alex = db.users.findOne({ email: "[email protected]" });
// Count
db.users.countDocuments({ age: { $gte: 18 } });

UPDATE operations

Atomic update operators.

css
// Update one (first match)
db.users.updateOne(
{ email: "[email protected]" },
{ $set: { name: "Alex Chen", updatedAt: new Date() } }
);
// Update many
db.users.updateMany(
{ active: false },
{ $set: { archivedAt: new Date() } }
);
// Common operators
// $set, set field
// $unset, remove field
// $inc, increment a numeric field
// $push, append to array
// $addToSet, append if not already present
// $pull, remove matching elements from array
// $currentDate, set to current date
// Example: increment a counter + push to array atomically
db.posts.updateOne(
{ _id: postId },
{
$inc: { viewCount: 1 },
$push: { recentViewers: { userId, at: new Date() } }
}
);
// Upsert (insert if not found)
db.users.updateOne(
{ email: "[email protected]" },
{ $set: { name: "New", createdAt: new Date() } },
{ upsert: true }
);

DELETE operations

Same pattern as update.

css
// Delete one
db.users.deleteOne({ email: "[email protected]" });
// Delete many
db.users.deleteMany({ archivedAt: { $exists: true } });
// Soft delete pattern (mark instead of remove)
db.users.updateOne(
{ _id: userId },
{ $set: { deletedAt: new Date() } }
);
// Then your app filters out { deletedAt: null }

💡 Atomic operators > read-modify-write

BAD: `const u = findOne(); u.views++; updateOne({_id}, {$set: u})`, lossy under concurrency. GOOD: `updateOne({_id}, { $inc: { views: 1 } })`, atomic at server. Mongo's operators are designed so common ops don't need round-trips to compute.

Common mistakes

Forgetting $set: `updateOne({_id}, { name: 'X' })` REPLACES the entire doc. Read-modify-write instead of atomic operators (lost-update bug). deleteMany({}), empties the collection (forgot the filter? Disaster). Not handling 'not found' on updateOne (matchedCount = 0 silently).

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←MongoDB Fundamentals
Back to MongoDB
Query Operators→