Partner with experts to accelerate your digital transformation journey
Bobcares

Getting Started with Node.js CRUD with Mongoose & MongoDB Atlas

PDF Header PDF Footer

Build and test Node.js CRUD with Mongoose & MongoDB Atlas. Learn setup, schema, and full CRUD APIs with Express and Postman in one guide. Our Live Support Team is always here to help you.

Getting Started with Node.js CRUD with Mongoose & MongoDB Atlas

Databases are the backbone of modern web applications. They hold everything from user data to product information and make it possible to create dynamic platforms. One of the most practical setups developers often use is Node.js CRUD with Mongoose & MongoDB Atlas. With this combination, you can quickly create, read, update, and delete records while keeping your project structured and scalable. Let’s walk through the entire process without dragging, keeping things clear and direct.

Node.js CRUD with Mongoose & MongoDB Atlas

What you need before starting

  • Familiarity with Express to run a server.
  • Experience in setting up REST APIs without authentication.
  • Postman installed to test endpoints.

This guide is verified with Node v15.3.0, npm v7.4.0, express v4.17.1, mongoose v5.11.12, and MongoDB v4.2.

Setting up MongoDB Atlas

First, create a free account on MongoDB Atlas. Then:

  • Deploy a free-tier cluster.
  • Add a database user and note the username and password.
  • Whitelist your IP under “Network Access.”
  • Copy the connection string from the “Clusters” section.

Example:

mongodb+srv://<username>:<password>@cluster0.mongodb.net/test?retryWrites=true&w=majority

Initialize Node project

Create the project folder and initialize:

mkdir nodejs-crud
cd nodejs-crud
npm init -y

Then, add a folder for source code:

mkdir src
cd src

Install Express and Mongoose

Install both with:

npm install express mongoose --save

Express handles routing while Mongoose connects Node.js with MongoDB Atlas.

Configure Postman

In Postman:

  • Create a collection for your project.
  • Add requests for POST, GET, PUT, and DELETE.
  • Test endpoints as you build them.

Build the server with Express

Set up the server on port 3000 and route /api for API requests.

Create Mongoose Schema

Inside studentschema.js: define the schema with fields like Id, Name, Roll, Birthday, and Address. Models will help you save, update, delete, and fetch data.

Connect MongoDB and set up routes

Create api.js:

const mongoose = require('mongoose');
const express = require('express');
const router = express.Router();
const StudentModel = require('./studentschema');
// Connecting to database
const query = 'mongodb+srv://Username:<password>'
+ '@student.tuufn.mongodb.net/College?'
+ 'retryWrites=true&w=majority'
const db = (query);
mongoose.Promise = global.Promise;
mongoose.connect(db, {
useNewUrlParser: true,
useUnifiedTopology: true
}, function (error) {
if (error) {
console.log("Error!" + error);
}
});
module.exports = router;
CRUD operations

Create (POST):

router.get('/save', function (req, res) {
const newStudent = new StudentModel({
StudentId: 101,
Name: "Sam", Roll: 1, Birthday: 2001 - 09 - 08
});
newStudent.save(function (err, data) {
if (err) {
console.log(error);
}
else {
res.send("Data inserted");
}
});
});

Using POST and body-parser:

router.post('/save', function (req, res) {
const newStudent = new StudentModel();
newStudent.StudentId = req.body.StudentId;
newStudent.Name = req.body.Name;
newStudent.Roll = req.body.Roll;
newStudent.Birthday = req.body.Birthday;
newStudent.save(function (err, data) {
if (err) {
console.log(error);
}
else {
res.send("Data inserted");
}
});
});
Read (GET):
router.get('/findall', function (req, res) {
StudentModel.find(function (err, data) {
if (err) {
console.log(err);
}
else {
res.send(data);
}
});
});
Delete (DELETE):
router.get('/delete', function (req, res) {
StudentModel.remove({ StudentId: 188 },
function (err, data) {
if (err) {
console.log(err);
}
else {
res.send(data);
}
});
});
router.post('/delete', function (req, res) {
StudentModel.findByIdAndDelete((req.body.id),
function (err, data) {
if (err) {
console.log(err);
}
else {
res.send(data);
console.log("Data Deleted!");
}
});
});
Update (PUT):
router.post('/update', function (req, res) {
StudentModel.findByIdAndUpdate(req.body.id,
{ Name: req.body.Name }, function (err, data) {
if (err) {
console.log(err);
}
else {
res.send(data);
console.log("Data updated!");
}
});
});

[If needed, Our team is available 24/7 for additional assistance.]

Conclusion

By now, you have a complete project that shows how to create, read, update, and delete records with Node.js CRUD with Mongoose & MongoDB Atlas. This approach keeps your project organized, makes data handling smoother, and is great for both beginners and intermediate developers. With Postman, you can test every endpoint confidently. Building projects with Node.js CRUD with Mongoose & MongoDB Atlas is not only efficient but also future-ready for scaling.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Get featured on the Bobcares blog and share your expertise with a global tech audience.

WRITE FOR US
server management

Spend time on your business, not on your servers.

TALK TO US

Or click here to learn more.

Speed issues driving customers away?
We’ve got your back!