-->

  • Make Get Delete and Put API with mongoose in Nodejs

     

    Make Get Delete and Put API with mongoose in Nodejs

     

      

    Make Get Delete and Put API with mongoose in Nodejs



    This blog we have seen mongodb basic and how is it work with installation and after installation, we have already seen mongodb basic, CRUD operation, connect mongodb with nodejs, read, update, delete, POST, GET API etc. we should not face any problem while making Database, collection(table) and using the data in nodejs even whenever we will make API. Read data from mongodb in nodejs



    • # What is Mongoose
    • # Install Mongoose
    • # Example of GET Method API
    • # Example of DELETE Method API
    • # Example of PUT Method API


     

    Now in this blog  we will use mongoose. There is an npm package to connect nodes to mongodb. Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. They created mongoose by improving mongodb, with the help of which you can create schemas and models of mongoose's movements.

    Suppose that there are 4 fields in our database (like name, brand, price, category) and the user wants to add a 5th field also (like color) but we want to add only 4 fields, for this we create schemas.

    Here you can also define TYPE in schemas like price type is numeric but if any user wants to use quotes (" " ' ') then we can stop the user for this.

    in sort we can apply validation in mongoose but we can't in directly mongodb.

     

     

     What is MongoDB

     

    MongoDB is a popular open-source NoSQL (non-relational) database management system. NoSQL means no structure like it is look like an object while in SQL it is a structure query language like SELECT CustomerName, City FROM Customers; (The SELECT statement is used to select data from a database.) But NoSQL database is useless...NOT completely because of whenever we want add extra column in database then it is possible through NoSQL MongoDB.   

     

    MongoDB installation - CLICK HERE

    MongoDB Basic Command - CLICK HERE

    About MongoDB vs SQL - CLICK HERE

    CRUD in MongoDB - CLICK HERE

     

     



    Object Like - 

     

     

    {
      "_id": {
        "$oid": "659ad46e0382148123c82841"
      },
      "name": "moto G60",
      "brand": "motorola",
      "price": 16500,
      "category": "mobile"
    }
    
    
    

     

     


    MongoDB is a type of database that helps store and manage large amounts of data in a flexible and scalable way. Unlike SQL, MongoDB doesn't require a fixed structure for the data, allowing you to store information in a more versatile format.


    • MongoDB is a NoSQL database.
    • The data stored in a collection
    • Collection don't have row and columns
    • Data is stored in the form of object.




    Install Mongoose


    For this you need to copy this (npm i mongoose) and paste it in project's terminal.

    https://www.npmjs.com/package/mongoose

     

     

     

    Make Get Delete and Put API with mongoose in Nodejs

     

     CLICK HERE FOR POST METHOD - POST WITH MONGOOSE


    Example of GET Method API

     

    Here, we gonna write the code of get method with mongoose, you can also check the post method with mongoose. simple endpoint is our /list and we will promises (async and await)

     

     

    app.get('/list', async (req, resp) => {
       let data  = await Product.find();
       resp.send(data)
    })
    

     

     

     if we run this code with the help of nodemon index.js or node index.js you can see this.



    Make Get Delete and Put API with mongoose in Nodejs


     

     Example of DELETE Method API

     

     Now we are using delete method and inside the parameter we send the ID and through this ID we will delete the data from database.

    Here we first simple console.log(done) checking for delete method is working or not.



    app.delete('/delete/:id', async (req, resp) =>{
       console.log(req.params);
    
       resp.send("Done")
    })
    
    
    



     

    Make Get Delete and Put API with mongoose in Nodejs

     

    app.delete('/delete/:_id', async (req, resp) =>{
       console.log(req.params);
       let data = await Product.deleteOne({id : 
       req.params})
       resp.send(data)
    })
    
    

     

     

     Here we breakdown the code and /delete/:_id this _id we will get from mongoDB dont worry i will showing you and we will use deleteOne method because of we  want to delete the only one data from id number and we will get id from parameters.



    Make Get Delete and Put API with mongoose in Nodejs


    Make Get Delete and Put API with mongoose in Nodejs


     Here, is acknowledged is true thats mean data is deleted.



     

     Example of PUT Method API

     

    With the PUT Method you can change the data means update the data through the _id, name whatever you want but mostly we will delete by _id.

     

     

    app.put("/update/:_id", async (req, resp) => {
       console.log(req.params)
       let data = await Product.updateOne(
           req.params, //it is condition 
          {
             $set : req.body
          }
          );
          resp.send(data)
    })
    

     

     updateOne method and req.params (it is condition ) and second one $set is represent what do you want to update. so in postman we will update the price of id.


    Make Get Delete and Put API with mongoose in Nodejs

    Make Get Delete and Put API with mongoose in Nodejs

    Make Get Delete and Put API with mongoose in Nodejs


     


     



    Disclaimer



    All tutorials are for informational and educational purposes only and have been made using our own routers, servers, websites and other vulnerable free resources. we do not contain any illegal activity. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. Hacking Truth is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used. We do not promote, encourage, support or excite any illegal activity or hacking.

     

  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.