CRUD Operations

Node js 12 min min read Updated: Mar 30, 2026 Intermediate
CRUD Operations
Intermediate Topic 3 of 10

CRUD Operations with Mongoose

CRUD stands for Create, Read, Update, and Delete. These are the four basic operations used to manage data in any database-driven application.

In Node.js applications that use MongoDB, CRUD operations are commonly performed with Mongoose. Mongoose makes these operations easier by providing a structured way to work with MongoDB collections using models and schemas.

Key Concept: CRUD operations allow you to insert new data, fetch existing records, modify data, and remove records from the database.

What are CRUD Operations?

CRUD is the foundation of almost every backend system. Whether you are building a blog, e-commerce application, admin panel, or user management system, these four operations are used regularly.

  • Create: Add new data to the database
  • Read: Retrieve data from the database
  • Update: Modify existing data
  • Delete: Remove data from the database

Example Mongoose Model

Before performing CRUD operations, let us define a simple Mongoose model:

javascript const mongoose = require("mongoose"); const userSchema = new mongoose.Schema({ name: { type: String, required: true }, email: { type: String, required: true }, age: Number }); const User = mongoose.model("User", userSchema);

Create Operation

The Create operation is used to insert new data into the database.

javascript const user = new User({ name: "Rahul", email: "rahul@example.com", age: 25 }); user.save() .then(data => console.log(data)) .catch(err => console.log(err));

You can also use Model.create() as a shorter alternative:

javascript User.create({ name: "Amit", email: "amit@example.com", age: 28 });

Read Operation

The Read operation is used to fetch data from the database.

Find All Records

javascript User.find() .then(users => console.log(users)) .catch(err => console.log(err));

Find One Record

javascript User.findOne({ email: "rahul@example.com" }) .then(user => console.log(user)) .catch(err => console.log(err));

Find By ID

javascript User.findById("64f123abc456789xyz000111") .then(user => console.log(user)) .catch(err => console.log(err));

Update Operation

The Update operation is used to modify existing records in the database.

Update One Record

javascript User.updateOne( { email: "rahul@example.com" }, { age: 30 } ) .then(result => console.log(result)) .catch(err => console.log(err));

Find By ID and Update

javascript User.findByIdAndUpdate( "64f123abc456789xyz000111", { name: "Rahul Sharma" }, { new: true } ) .then(user => console.log(user)) .catch(err => console.log(err));

The new: true option returns the updated document instead of the old one.

Delete Operation

The Delete operation is used to remove records from the database.

Delete One Record

javascript User.deleteOne({ email: "rahul@example.com" }) .then(result => console.log(result)) .catch(err => console.log(err));

Find By ID and Delete

javascript User.findByIdAndDelete("64f123abc456789xyz000111") .then(user => console.log(user)) .catch(err => console.log(err));

CRUD Operations in Express Routes

In real-world applications, CRUD operations are usually connected to API routes.

javascript app.post("/users", async (req, res) => { const user = await User.create(req.body); res.json(user); }); app.get("/users", async (req, res) => { const users = await User.find(); res.json(users); }); app.put("/users/:id", async (req, res) => { const user = await User.findByIdAndUpdate(req.params.id, req.body, { new: true }); res.json(user); }); app.delete("/users/:id", async (req, res) => { await User.findByIdAndDelete(req.params.id); res.json({ message: "User deleted successfully" }); });

Why CRUD Operations Matter

CRUD operations are the backbone of backend development. Every dynamic application needs a way to store, retrieve, modify, and remove data.

Once you understand CRUD well, you can build admin panels, dashboards, APIs, and full-stack applications much more confidently.

Best Practices

  • Validate data before creating or updating records
  • Use proper error handling for all database operations
  • Return meaningful API responses
  • Use async/await for better readability
  • Check whether a record exists before updating or deleting it

Common Mistakes

  • Not handling database errors properly
  • Updating data without validation
  • Deleting records without checking permissions
  • Using incorrect IDs or filters in queries

Real-World Use Cases

  • User management systems
  • Product and order management
  • Blog posts and comments
  • Admin dashboards
  • Inventory systems

Conclusion

CRUD operations are one of the most essential concepts in backend development. With Mongoose, performing create, read, update, and delete operations becomes simple and structured.

If you are building applications with Node.js and MongoDB, mastering CRUD operations is a must. It gives you the foundation needed to build real-world APIs and data-driven systems.

Quick Summary: CRUD operations let you create, read, update, and delete data in MongoDB, and Mongoose provides simple methods to perform these tasks efficiently.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators