How to Perform CRUD Operations With MongoDB and Node.js
CRUD is the bread and butter of backend development. Here is how to do it with MongoDB and Node.js.
How to Perform CRUD Operations With MongoDB and Node.js
CRUD (Create, Read, Update, Delete) is the bread and butter of backend development. Here is how to do it with MongoDB and Node.js.
Create
User.create({ name, email }) creates a new document. You can also create with const user = new User(data); await user.save(). Both work; create is a convenience that does both.
Read
User.find() returns all documents. User.findOne({ email }) returns one. User.findById(id) returns one by id. Use queries, projections, and pagination for efficient reading.
Update
User.findByIdAndUpdate(id, { name: 'new' }) updates and returns the updated document (with { new: true }). User.updateOne({ email }, { $set: { name } }) updates without fetching first.
Delete
User.findByIdAndDelete(id) deletes by id. User.deleteOne({ email }) deletes by query. User.deleteMany({ status: 'inactive' }) deletes all matching documents.
Query Operators
MongoDB supports operators: $eq, $ne, $gt, $lt, $in, $nin, $and, $or, $not. For example, User.find({ age: { $gt: 18 } }) finds users older than 18.
Pagination
For pagination, use skip and limit: User.find().skip(page * pageSize).limit(pageSize). For large datasets, use cursor-based pagination with the id for better performance.
The Takeaway
CRUD with MongoDB and Node.js: create with Model.create or save, read with find/findOne/findById, update with findByIdAndUpdate or updateOne, delete with findByIdAndDelete or deleteOne. Use query operators for filtering and pagination for large datasets.
Create with Model.create or save, read with find/findOne/findById, update with findByIdAndUpdate or updateOne, delete with findByIdAndDelete or deleteOne. Use query operators for filtering and pagination for large datasets.
Use User.create({ name, email }) or create a new instance and save: const user = new User(data); await user.save(). Both create a new document. User.create is a convenience that does both in one step.
User.find() returns all, User.findOne({ email }) returns one by query, User.findById(id) returns one by id. Use query operators like $gt, $lt, $in, $and, $or for filtering: User.find({ age: { $gt: 18 } }).
Use skip and limit: User.find().skip(page * pageSize).limit(pageSize). For large datasets, use cursor-based pagination with the id for better performance, since skip slows down on large offsets.
User.findByIdAndUpdate(id, { name: 'new' }, { new: true }) updates and returns the updated document. Use { new: true } to get the updated document back, not the original. Or use updateOne for updates without fetching first.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

