Skip to main content

APIs for mongoDB data to Update, Insert , Delete, Get Data ( Postman API Testing also )

 const express = require('express');

const app = express();

const { MongoClient, ObjectId } = require('mongodb')


// API for get data start

const url = "mongodb://localhost:27017/";

app.get('/', (req, res) =>{

  MongoClient.connect(url, function(err, db) {

    if (err) throw err;

    var dbo = db.db("vasu"); // here vasu is database name

    dbo.collection("tnl_register").find().toArray(function(err, result) {

      if (err) throw err;

      console.log(result);

      res.send(result);

      db.close();

    });

  });

});



// API for get one record start

app.get('/:id', (req, res) =>{

  MongoClient.connect(url, function(err, db) {

    if (err) throw err;

    var dbo = db.db("vasu");

    const query = {"_id": ObjectId(req.params.id)};

    dbo.collection("tnl_register").findOne(query, (err, result) => {

      if (err) throw err;

      console.log(result);

      res.send(result);

      db.close();

    });

  });

});

// API for get one record end


// API for insert one record start

app.post('/', (req, res) =>{

  MongoClient.connect(url, function(err, db) {

    if (err) throw err;

    var dbo = db.db("vasu"); // here vasu is database name db is returned by callface fucntion and another db is predefined 

    const query = {name: req.headers.name, email: req.headers.email, mobile: req.headers.mobile };

    dbo.collection("tnl_register").insertOne(query, (err, result) => {

      if (err) throw err;

      console.log(result);

      res.send('Inserted ID : '+result.insertedId);

      db.close();

    });

  });

});

// API for insert one record end


// API for delete one record start

app.delete('/:id', (req, res) =>{

  MongoClient.connect(url, function(err, db) {

    if (err) throw err;

    var dbo = db.db("vasu"); //here vasu is database name and another db is predefined

    const query = {_id: ObjectId(req.params.id)};

    dbo.collection("tnl_register").deleteOne(query, (err, result) => {

      if (err){

        res.send('error');

      }else{

        if(result.deletedCount === 1){

          res.send('One record deleted!');

        }else{

          res.send('not delete!');

        }

      }

      db.close();

    });

  });

});

// API for delete one record end


// API for update one record start

app.put('/:id', (req, res) =>{

  MongoClient.connect(url, function(err, db) {

    if (err) throw err;

    var dbo = db.db("vasu"); // here vasu is database name and another db is predefined.

    const id = {_id: ObjectId(req.params.id)};

    const query = {$set: {name: req.headers.name, email: req.headers.email, mobile: req.headers.mobile }};

    dbo.collection("tnl_register").updateOne(query, id, (err, result) => {

      if (err){

        res.send('error');

      }else{

        res.send(result);

      }

      db.close();

    });

  });

});

// API for update one record end


app.listen(3000)

Comments

Popular posts from this blog

Part 15- What is Repeater (Networking Devices)- Computer Networking- CCNA

Repeater  hello friends i am Vasu Birla and today i am starting new segment of CCNA computer networking  its a Networking Devices. we will discuss every important Devices used in networks. Repeater is a first networking devices. What is Repeater  Repeater is a device that receives signals and re-transmits by amplifying or regenerating signals. Repeater has two port one is for receive signals from previous network and second port is for retransmit signals to the next extended network. Two Port Repeater Repeater is a device which is used to Regenerate or replicate signal (Pichhe se aa rahe signal ko regenerate karke ya amplify karke aage strong signals bhej sakte he) Analog and digital both type of signals can be retransmitted by repeater. Here there is difference between regeneration and amplifying.  Amplification means , received signals will be amplified as it is , whether there are impurities in signals  and Regeneration means repeate...

How to Kill the Process: ( Port Process )

If you are seeing a Error like  -   Error: listen EADDRINUSE: address already in use :::3000   then you can easily kill it by running these commands  Find the Process Using Port 3006 : On Windows   netstat -ano | findstr :3000 On Linux/macOS lsof -i :3000       Terminate the Process :       On Windows taskkill /PID <PID> /F    means       taskkill /PID 17780 /F On Linux/macOS kill -9 <PID>     means kill -9 17780                ==============END ==========  

GitHub Repo Collaboration Work on single project

 =============================================== To collaborate effectively with your friend on the same project, you should use Git branches to manage different lines of development. Here's a step-by-step procedure you can follow to streamline collaboration: 1. Create Separate Branches for Each Developer Create a New Branch for Your Friend: On your local repository, create a new branch for your friend. For example if your friend name is kilvish , if you want to create a branch named kilvish , you would run: make sure you would be on main branch already   command->  git checkout -b kilvish git push origin kilvish 2. Set Up Your Friend’s Environment( On your Friend's System )  at kilvish side  run ->  Clone the Repository (if not already done): If your friend hasn’t cloned the repository yet, they should do so: command ->  git clone https://github.com/Vasu-Birla/your-repo.git   // your your main clone line  Fetch All Bran...