-->

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.

  • Delete API delete data from MongoDB

     

     



    Delete API delete data from 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;
    

     

     




















  • Basic GET API with MongoDB

     

    Basic GET API with MongoDB

     

     

    Basic Get API 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



    • # Make POST method for API
    • # Send data from postman
    • # Get data in nodejs by request
    • # write code for insert data in MongoDB


     

    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.

     

     

    const express = require('express')
    const dbConnect = require('./mongodb/mongodb')
    
    const app = express();
    
    app.get('/', async(req, resp) => {
        let data = await dbConnect();
        data = await data.find().toArray();
        console.log(data)
        resp.send({ name : "Atul" })
    });
    
    app.listen(5000)
    
    

     

     


    app.get() method we can also say it is routing method. Here we are combining express with mongoDB.


    req parameter we have to used for to Get Data in API.
    resp parameter will respond to the API.

    when we run in our browser with the port 5000 and in our browser are displaying json data.

     

    If you want more information about async and await - CLICK HERE

     


    Basic GET API with MongoDB


     

    Basic GET API with MongoDB

     

     

    if we change something in one line of code like this resp.send(data) and refresh the browser and see in your browser and vs code terminal.

     

     

    Basic GET API with MongoDB

     

     

     if we check in Postman  you can check with api url like http://localhost:5000/



     

    Basic GET API with 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.

     


  • Update data from mongodb in nodejs

     

    Update data from mongodb in nodejs

     

     

     

     

    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



    • # Make new file for update data
    • # import mongodb connection
    • # update single and multi records


     

    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 update data

     

    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.

    otherwise we will create a new file called as update.js file. Okay!!

     

     

     

    const dbConnect = require('./mongodb/mongodb')
    
    const updateData = async () => {
        // console.log("updateData function called")
        let data = await dbConnect();
        let result = await data.updateOne(
            {name: 'moto G60' }, {
            $set: { price: 25000 }
        }
        );
        console.log(result)
        
    }
    
    updateData();
    

     

     

     

    Before updating data

     

     


    Update data from mongodb in nodejs


     

    Here, our code is saying that first we simply connect mongodb and after that we will make function (updateData()) function and inside this code condition.

     

      {name: 'moto G60' }, {
      $set: { price: 25000 }
    }


    first condition is saying that in which parameter through you want to update the data and after that $set thats mean what do you want to change. Like price's value.


     

     when we will run this code with nodemon update.js file.




    Update data from mongodb in nodejs



    Update ManyData

     

    If you want update many data at a time you can do this by updateMany. 

     

     

    const dbConnect = require('./mongodb/mongodb')
    
    const updateData = async () => {
        // console.log("updateData function called")
        let data = await dbConnect();
        let result = await data.update(
            {name: 'moto G60' }, {
            $set: { name: 'vivo 5g' , price: 25000 }
        }
        );
        console.log(result)
        
    }
    
    updateData();
    

     

     

     

    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.

     

  • Delete data from mongodb in nodejs

     

    Delete data from mongodb in nodejs

     

     

    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.




     Read Data from MongoDB

     

    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.

     

    Now we will create the deleteData() function with condition of delete the data.

     

     

    const dbConnect = require('./mongodb/mongodb')
    
    const deleteData = async () => {
        // console.log("deleteData function is called")
        let data = await dbConnect();
        let result = await data.deleteOne({
             name: 'moto G60'
        })
        console.log(result)
    }
    
    deleteData();
    

     

     

     

    Delete data from mongodb in nodejs

     

     with this condition if result.acknowledged is true then print console data.



    const dbConnect = require('./mongodb/mongodb')
    
    const deleteData = async () => {
        // console.log("deleteData function is called")
        let data = await dbConnect();
        let result = await data.deleteOne({
             name: 'moto G60'
        })
        console.log(result)
        if(result.acknowledged)
        {
            console.log("records deleted") 
        }
    }
    
    deleteData();
    

     


     


    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.

     

     

     

  • Insert data from mongodb in nodejs

     

    Insert data from mongodb in nodejs

     

     

    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



    • # Make New File for Insert data
    • # Import MongoDB connection
    • # Insert single and multiple records


     

     

     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.




    Before jump into inserting data so check read data from mongodb and connection blog.


    Connection nodejs with mongodb - CLICK HERE

    Read Data from mongodb - CLICK HERE


     Insert Data from MongoDB

     

    We will write code of insert data in separate file so first make a new file for inserting data

     

     

     

    const dbConnect = require('./mongodb/mongodb')
    
    
    const insert = () => {
        console.log("Insert Function")
    }
    
    
    insert();
    

     

     

    Insert data from mongodb in nodejs

     

     

    Run with nodemon insert.js

     

    Whole Code 

     

     

    const dbConnect = require("./mongodb/mongodb");
    
    const insert = async () => {
      const db = await dbConnect();
      const result = await db.insertOne({
        name: "note 5",
        brand: "vivo",
        price: 2500,
        category: "mobile",
      });
    
      console.log("data inserted");
      console.log(result);
    };
    
    insert();
    
    

     

     

    if you want insert too much data in one command you can use insertMany() function.

     

    InsertMany()

     


    // const dbConnect = require("./mongodb/mongodb");
    
    // const insert = async () => {
    //   const db = await dbConnect();
    //   const result = await db.insertOne({
    //     name: "note 5",
    //     brand: "vivo",
    //     price: 2500,
    //     category: "mobile",
    //   });
    
    //   console.log("data inserted");
    //   console.log(result);
    // };
    
    // insert();
    
    const dbConnect = require("./mongodb/mongodb");
    
    const insert = async () => {
      const db = await dbConnect();
      const result = await db.insertMany([
        {
          name: "note 5",
          brand: "vivo",
          price: 2500,
          category: "mobile",
        },
        {
          name: "micro",
          brand: "micromax",
          price: 4500,
          category: "mobile",
        },
      ]);
    
      console.log("data inserted");
      console.log(result);
    };
    
    insert();
    
    



     

    Insert data from mongodb in nodejs

     

     

    output - data inserted, acknowledged : true, insertCount: 2

     

     


    Insert data from mongodb 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.

     

     

     

  • Read data from mongodb in nodejs

     

    Read data from mongodb in nodejs

     

     

     

    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.




     Read Data from MongoDB

     

    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.

     

    First you need to create folder for mongoDB connection and inside this folder we will create mongodb.js file.

     

     

     

    Read data from mongodb in nodejs

     

     

     
    Now we will write code in index.js for importing mongodb client and some code.


    1. Importing MongoDB and Setting Up Connection:


    const { MongoClient } = require('mongodb');
    const url = 'mongodb://127.0.0.1:27017';


    Here, you are importing the MongoClient class from the 'mongodb' package, which is the official MongoDB driver for Node.js. You define the connection URL for your MongoDB server. In this case, it's mongodb://127.0.0.1:27017, which is the default address and port for a locally running MongoDB server.



    2. Defining Database and Creating a MongoClient:


    const databaseName = 'e-comm';
    const client = new MongoClient(url);



    You specify the name of the database you want to connect to (e-comm), and then you create a new instance of the MongoClient using the provided connection URL.





    3. Asynchronous Function to Retrieve Data:




    async function dbConnect() {
      let result = await client.connect();
      let db = result.db(databaseName);
      return db.collection("products")
     
    }



    # This function, getData, is marked as async, indicating that it contains asynchronous operations.

    # await client.connect();: It connects to the MongoDB server asynchronously and returns a result. This result is an instance of the MongoClient connected to the MongoDB server.

    # let db = result.db(databaseName);: It retrieves the specified database (e-comm in this case) from the connected MongoClient.

    # let collection = db.collection('products');: It gets a reference to the 'products' collection within the 'e-comm' database.


    4. exports the dbConnect:


    module.exports = dbConnect;


    The module concludes by exporting the dbConnect. When executed, it will connect to the MongoDB server, retrieve data from the 'products' collection, and log the result to the console.


     

    Whole Code (/mongodb/mongodb)

     

     

    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 we will write the code in index.js file for read the data from mongodb.



    const dbConnect = require('./mongodb/mongodb')
    
    
    const main = async () => {
      let data = await dbConnect();
      data = await data.find({}).toArray();
      console.log(data)
    }
    
    main();
    

     

     

    Here we first import by const dbConnect with require('./mongodb/mongodb') and promises handle with async and await.


    If you want more information about async and await - CLICK HERE

     

     

    Read data from mongodb 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.





  • Connect nodejs with mongodb

     

    Connect node with mongodb

     

     

    Mongodb basic command



    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.



    • # Install MongoDB Package
    • # Connect MongoDB with Nodejs
    • # Show data from mongoDB


     

    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.




    Now you can understand how can we connect nodejs with the mongodb and how can we use nodejs with the help of mongodb and data insert, update, delete, update dynamic data or make API

     and we have some data in mongodb so after connecting mongodb with the nodejs we can show data from mongodb in nodejs.




    Connect node with mongodb


    we can install this driver as a pkg but it is driver YEAH! because of we can connect nodejs and mongodb with the help of this driver pkg OKAY.

    The official MongoDB driver for Node.js. - https://www.npmjs.com/package/mongodb ( npm i mongodb )

     

     

     Connect node with mongodb

     

     

    Connect MongoDB with Nodejs

     

     Now we will write code in index.js for importing mongodb client and some code.


    1. Importing MongoDB and Setting Up Connection:


    const { MongoClient } = require('mongodb');
    const url = 'mongodb://127.0.0.1:27017';



    Here, you are importing the MongoClient class from the 'mongodb' package, which is the official MongoDB driver for Node.js. You define the connection URL for your MongoDB server. In this case, it's mongodb://127.0.0.1:27017, which is the default address and port for a locally running MongoDB server.



    2. Defining Database and Creating a MongoClient:


    const database = 'e-comm';
    const client = new MongoClient(url);




    You specify the name of the database you want to connect to (e-comm), and then you create a new instance of the MongoClient using the provided connection URL.





    3. Asynchronous Function to Retrieve Data:



    async function getData() {
      let result = await client.connect();
      let db = result.db(database);
      let collection = db.collection('products');
      let response = await collection.find({}).toArray();
      console.log(response);
    }




    # This function, getData, is marked as async, indicating that it contains asynchronous operations.

    # await client.connect();: It connects to the MongoDB server asynchronously and returns a result. This result is an instance of the MongoClient connected to the MongoDB server.

    # let db = result.db(database);: It retrieves the specified database (e-comm in this case) from the connected MongoClient.

    # let collection = db.collection('products');: It gets a reference to the 'products' collection within the 'e-comm' database.

    # let response = await collection.find({}).toArray();: It performs a query to find all documents in the 'products' collection and converts the result to an array.

    # console.log(response);: Finally, it logs the array of documents (or an empty array if there are no documents) to the console.



    4. Calling the getData Function:


    getData();




    The script concludes by calling the getData function. When executed, it will connect to the MongoDB server, retrieve data from the 'products' collection, and log the result to the console.



    Whole Code 

     

    const {MongoClient} = require('mongodb');
    const url = 'mongodb://127.0.0.1:27017'
    
    const database = 'e-comm'
    const client = new MongoClient(url);
    
    async function getData()
    {
      let result = await client.connect();
      let db = result.db(database);
      let collection = db.collection('products');
      let response = await collection.find({}).toArray();
      console.log(response);
    }
    
    getData();
    

     

     

    Run through this command - nodemon index.js or node index.js



     

    Connect node with 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.