Mastering Node.js: Building Scalable Web Applications
Course Title: Mastering Node.js: Building Scalable Web Applications Section Title: Managing Data with MongoDB and Mongoose Topic: Performing CRUD operations with Mongoose
Overview
In this topic, we will explore how to perform Create, Read, Update, and Delete (CRUD) operations using Mongoose, a popular Object Data Modeling (ODM) library for MongoDB and Node.js. We will cover the basics of CRUD operations, including how to create, read, update, and delete documents in a MongoDB database using Mongoose.
Prerequisites
Before diving into this topic, make sure you have a basic understanding of:
- Node.js and its ecosystem
- MongoDB and its data model
- Mongoose and its features
- Setting up a MongoDB and Mongoose in Node.js
What are CRUD Operations?
CRUD operations are the basic operations that can be performed on data in a database. They are:
- Create: Create a new document in the database
- Read: Retrieve a document from the database
- Update: Update an existing document in the database
- Delete: Delete a document from the database
Performing CRUD Operations with Mongoose
Mongoose provides a simple and intuitive API for performing CRUD operations on MongoDB documents. Here are some examples of how to perform CRUD operations using Mongoose:
Create
To create a new document in the database, you can use the create()
method on a Mongoose model. Here is an example:
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: String,
email: String
});
const User = mongoose.model('User', userSchema);
const user = new User({ name: 'John Doe', email: 'john.doe@example.com' });
user.save((err, user) => {
if (err) {
console.error(err);
} else {
console.log(user);
}
});
Read
To retrieve a document from the database, you can use the find()
method on a Mongoose model. Here is an example:
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: String,
email: String
});
const User = mongoose.model('User', userSchema);
User.find({ name: 'John Doe' }, (err, users) => {
if (err) {
console.error(err);
} else {
console.log(users);
}
});
Update
To update an existing document in the database, you can use the update()
method on a Mongoose model. Here is an example:
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: String,
email: String
});
const User = mongoose.model('User', userSchema);
User.update({ name: 'John Doe' }, { $set: { email: 'john.doe2@example.com' } }, (err, user) => {
if (err) {
console.error(err);
} else {
console.log(user);
}
});
Delete
To delete a document from the database, you can use the remove()
method on a Mongoose model. Here is an example:
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: String,
email: String
});
const User = mongoose.model('User', userSchema);
User.remove({ name: 'John Doe' }, (err, user) => {
if (err) {
console.error(err);
} else {
console.log(user);
}
});
Conclusion
In this topic, we covered the basics of CRUD operations using Mongoose. We learned how to create, read, update, and delete documents in a MongoDB database using Mongoose. We also saw some examples of how to perform CRUD operations using Mongoose.
What's Next?
In the next topic, we will explore the principles of RESTful architecture and how to build RESTful APIs using Node.js and Express.js.
Leave a comment or ask for help if you have any questions or need further clarification on any of the concepts covered in this topic.
External Resources:
- Mongoose documentation: <https://mongoosejs.com/docs/>
- MongoDB documentation: <https://docs.mongodb.com/>
- Node.js documentation: <https://nodejs.org/docs/>
Practice Exercise:
Create a Mongoose model for a Book
document with the following schema:
const bookSchema = new mongoose.Schema({
title: String,
author: String,
publicationDate: Date
});
Then, perform the following CRUD operations using the Book
model:
- Create a new
Book
document with the title "The Great Gatsby" and author "F. Scott Fitzgerald". - Retrieve all
Book
documents with the title "The Great Gatsby". - Update the publication date of the
Book
document with the title "The Great Gatsby" to 1925. - Delete the
Book
document with the title "The Great Gatsby" title.
Images

Comments