Stop 05 · MongoDB FundamentalsTopics · 5/9

CRUD Operations in MongoDB

Part of the MERN Stack Roadmap

In short

Create, read, update, delete: the same four jobs as SQL, with MongoDB's own verbs. Learn the filter and update operators and you can write most API endpoints.

insertOne, find, updateOne and deleteOne (plus their Many versions) create, read, update and delete documents. These map directly to the POST, GET, PATCH and DELETE routes of your API.

The blob pins, swaps and removes coloured cards on a hostel cork board.

Think of it like this

The hostel notice board

Pin a new notice (create). Scan for anything about the cricket match (read). Change the date on the fest notice (update). Pull down last month's water-cut warning (delete). MongoDB's CRUD methods are those four moves, done with filters instead of your eyes.

Watch it happen

Managing notices

API
MongoDB

insertOne({ title… })

1/6Create: add one notice. MongoDB gives it an _id.

The big ideas

What you need to know

insertOne and insertMany

Add documents. insertMany is much faster than looping insertOne for bulk data.

find with operators

Filters use operators like $gt, $in and $regex. { year: { $gt: 2 } } means year above 2.

Update operators

$set changes fields, $inc adds to numbers, $push appends to arrays. Use replaceOne only to swap a whole document.

deleteOne and deleteMany

Careful: an empty filter {} matches everything, so deleteMany({}) empties the collection.

See it in code

Walk through the code

ts
1const notices = db.collection("notices");2 3await notices.insertOne({ title: "Fest on Friday", tag: "events", likes: 0, createdAt: new Date() });4 5const latest = await notices6  .find({ tag: { $in: ["sports", "events"] } })7  .sort({ createdAt: -1 })8  .limit(10)9  .toArray();10 11await notices.updateOne({ title: "Fest on Friday" }, { $set: { title: "Fest on Saturday" }, $inc: { likes: 1 } });12 13await notices.deleteMany({ expiresAt: { $lt: new Date() } });

1/4Create one notice. The driver adds an _id if you don't.

db here is client.db('college') from the official mongodb driver. Every method returns a promise.

Save yourself the pain

Common mistakes

Oops

Reading a document, changing it in JavaScript, then saving the whole thing back.

Do this instead

Two requests can overwrite each other. Use $set or $inc so MongoDB changes just those fields atomically.

Oops

Passing user input straight into a filter, like find(req.body).

Do this instead

A crafted body like { $ne: null } can match everything. Validate input and build filters yourself.

Your mission

Notice board API

~30 min

Quick check

Which update safely adds 1 to a likes counter when many users click at once?

Go deeper

  1. 1Insert ten products, then find, update and delete a few of them in mongosh.
  2. 2Use findOne when you expect one document and find when you expect a list.
  3. 3Check the result objects, such as matchedCount and modifiedCount, after updates.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge