-->

ABOUT US

Our development agency is committed to providing you the best service.

OUR TEAM

The awesome people behind our brand ... and their life motto.

  • Kumar Atul Jaiswal

    Ethical Hacker

    Hacking is a Speed of Innovation And Technology with Romance.

  • Kumar Atul Jaiswal

    CEO Of Hacking Truth

    Loopholes are every major Security,Just need to Understand it well.

  • Kumar Atul Jaiswal

    Web Developer

    Techonology is the best way to Change Everything, like Mindset Goal.

OUR SKILLS

We pride ourselves with strong, flexible and top notch skills.

Marketing

Development 90%
Design 80%
Marketing 70%

Websites

Development 90%
Design 80%
Marketing 70%

PR

Development 90%
Design 80%
Marketing 70%

ACHIEVEMENTS

We help our clients integrate, analyze, and use their data to improve their business.

150

GREAT PROJECTS

300

HAPPY CLIENTS

650

COFFEES DRUNK

1568

FACEBOOK LIKES

STRATEGY & CREATIVITY

Phasellus iaculis dolor nec urna nullam. Vivamus mattis blandit porttitor nullam.

PORTFOLIO

We pride ourselves on bringing a fresh perspective and effective marketing to each project.

Showing posts with label PUT API Update data in MongoDB. Show all posts
Showing posts with label PUT API Update data in MongoDB. Show all posts
  • Post API insert data in MongoDB

     

    Post API insert data in MongoDB


     

    Post API insert data in MongoDB



    This blog and in upcoming blog we will see mongodb basic and how is it work with installation and after installation 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



    • # Read Data from MongoDB
    • # Make file for db connection
    • # Handle Promise 


     

    Now in this blog we will consider about mongoDB basic command which we can use to create Database, collection etc.


     

     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.





     

    Make new file for API

     

    If you remember, we wrote the code for database connection and data reading in the same file. if you write coding of insert, update, delete in same function and same file then it will be very complex. so that's why we will separate file of mongoDB connection file. Like this - 

     

    mongodb.js

     

     

    const { MongoClient } = require('mongodb');
    const uri = 'mongodb://127.0.0.1:27017/';
    const client = new MongoClient(uri);
    const dbName = 'e-comm';
    
    async function dbConnect()
    {
        //handle promises
        let result = await client.connect();
        console.log(result)
        db = result.db(dbName);  
        return db.collection("products")
    }
    
    module.exports = dbConnect;
    

     

     

    Now after making a new file (api.js) we will write code for get api.

     

     

    app.post('/', async (req, resp) => {
        resp.send({ name: "Atul Kumar" })
    })
    
    

     

     

    if we test this API  in postmand like this http:localhost:5000/ with post method.

    you can simple run this api with nodemon api.js

     

     

    Post API insert data in MongoDB

     

     

     

    Post API insert data in MongoDB

     


     

    According to above the image if we give json data in postman then how can we know this data comes or not in nodejs. so for this we will some changes and add middleware.


    this middleware used for (app.use(express.json()) - convert data in json format in nodejs.


    app.post('/', async (req, resp) => {
        console.log(req.body)
        resp.send({ name: "Atul Kumar" })
    })
    
    


    console.log(req.body) 

     

    if we send the data from postman then we can check that in your vs code terminal.


    Post API insert data in MongoDB


    Now how can we saved this data in MongoDB



    app.post('/', async (req, resp) => {
    
        let data = await dbConnect();
        let result = await data.insertOne(req.body);
        
        resp.send(result)
    })
    

     

     

     

     

    Post API insert data in MongoDB

     

     After send the data using postman and check the mongodb.

     

     

    Post API insert data in MongoDB

     

     

     


    Post API insert data in MongoDB


     

     


    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.

     



     

  • WHAT WE DO

    We've been developing corporate tailored services for clients for 30 years.

    CONTACT US

    For enquiries you can contact us in several different ways. Contact details are below.

    Hacking Truth.in

    • Street :Road Street 00
    • Person :Person
    • Phone :+045 123 755 755
    • Country :POLAND
    • Email :contact@heaven.com

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.