CRUD Operations using MongoDB in Node js express
This article helps you to understand, how to perform CRUD operations in NodeJs using MongoDB(not mongoose) with examples and how to test.
Pre-requisites
- Familiar with Node js and MongoDB and are installed in your machine.
- You can use Visual studio code to develop your projects.
- Install Postman to test your code
Terminology
Express
Express is a framework which runs on top of Node js for building web applications and it is more preferable as it simplifies server creation process
MongoDB
MongoDB is a no-sql database which is used widely now a days.
CRUD
CRUD is a acronym for Create, read, update and delete operations
How to start
- create a folder in your repository
- Open terminal and navigate to the folder you created.
- execute
npm init
, which creates package.json file. - execute
code package.json
, to open the package.json in your visual studio code.
Dependencies
- Install below dependencies
npm i express mongodb body-parser --save
Code
Consider pets is a collection which has id, name and age information of pets in your local machine. You can perform CRUD operations on pets as below
const express = require('express');
const server = express();
var bodyParser = require('body-parser');
server.use(bodyParser.urlencoded({
limit: "4mb",
extended: false
}));
server.use(bodyParser.json({ limit: "4mb" }));
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
let db = null;
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
db = client.db('pets');
});
// read all pets
server.get('/pets', (req, res) => {
db.collection('pets').find({}).toArray(function(err, docs) {
res.send(docs);
});
});
// read one pet information using id
server.get('/pets/:id', (req, res) => {
console.log('req.params.id', req.params.id);
db.collection('pets').findOne({_id: parseInt(req.params.id)}, function(err, doc){
if(err){
console.log(err);
res.send(err);
}
else{
console.log(doc);
res.send(doc);
}
});
});
// insert a pet
server.put('/pets', (req, res) => {
console.log('body', req.body);
db.collection('pets').insertOne({_id: req.body.id,name:req.body.name,age:req.body.age});
res.send("inserted successfully");
});
// update a pet
server.post('/pets/:id', (req, res) => {
console.log('id', req.body.id);
db.collection('pets').updateOne({_id: req.body.id},{$set:{name:req.body.name,age:req.body.age}});
res.send('updated successfully');
});
// delete pet
server.delete('/pets/:id', (req, res) => {
console.log('id', req.body.id);
db.collection('pets').deleteOne({_id: req.body.id});
res.send('deleted successfully');
});
server.listen(5000);
How to run your code
- switch to your terminal again or you can open terminal in your VS code itself.
- Execute the below command
node index.js
How to Test
1. Get or Read pets information
- Open your browser and hit
- to retrieve all pets information
http://localhost:5000/pets
- to retrieve one pet information
http://localhost:5000/pets/2
2. Insert or PUT pets information
- Open Postman
- Select PUT as protocol and
http://localhost:5000/pets
as URL - Configure the headers to send JSON data by making key as
Content-Type
and value asapplication/json
. - Send sample test data in body.
Sample test data:
{
"id": 3,
"name": "sunny",
"age": 3
}
- You can see the response in the bottom of the page.
3. Update or POST
- Open Postman
- Select POST as protocol and
http://localhost:5000/pets/1
as URL - Configure the headers to send JSON data by making key as
Content-Type
and value asapplication/json
. - Send sample test data in body.
Sample test data:
{
"name": "mypuppy",
"age": "10"
}
- You can see the response in the bottom of the page as
updated successfully
.
4. Delete
- Open Postman
- Select DELETE as protocol and
http://localhost:5000/pets/1
as URL and click Send. - You can see the response in the bottom of the page as
deleted successfully
.