MongoDB

 


Create a folder named mongo(anyname), create a file named mongoinsert.js, mongoupdate.js
& mongodelete.js.
Open terminal and run the following command --- npm i mongodb



After that add this code to mongoinsert.js.

const {MongoClient} = require("mongodb") // npm i mongodb

const uri = "mongodb://0.0.0.0:27017"; // Add Mongodb URL String here

const client = new MongoClient(uri);

async function run()
{
    try {
        const database = client.db("College"); // Give name of your Database
        const College = database.collection("students"); // Give name of your Collection

        const query = {
            // Insert Query here
        };


        // Perform CRUD Operations Here
        const result = await College.insertMany(
            [
                {
                    _Id: 21,
                    Name: "Meet",
                    Role: "Frontend Developer",
                    Salary: 2500000
                }
            ]
        );

        console.log("Database Connected")
        console.log(`${result.insertedCount} data is inserted.`);

    }
    finally {
        await client.close();
    }

}

run().catch(console.dir);


After that add this code to mongoupdate.js.

const { MongoClient } = require("mongodb");

const uri = "mongodb://0.0.0.0:27017"; // Add Mongodb URL String here

const client = new MongoClient(uri);

async function run() {
  try {
    const database = client.db("College"); // Give the name of your Database
    const College = database.collection("students"); // Give the name of your Collection

    const query = {
      // Define the query to specify which documents to update
      Name: "Meet", // For example, update students with the name "Rob"
    };

    const updateData = {
      $set: {
        Role: "Intern",
        Salary: 2500,
      },
    };

    // Perform CRUD Operations Here
    const result = await College.updateMany(query, updateData);
   
    console.log("Database Connected");
    console.log(`${result.modifiedCount} document(s) updated.`);

   
  } finally {
    await client.close();
  }
}

run().catch(console.dir);

After that add this code to mongodelete.js.

const { MongoClient } = require("mongodb");

const uri = "mongodb://0.0.0.0:27017"; // Add Mongodb URL String here

const client = new MongoClient(uri);

async function run() {
  try {
    const database = client.db("College"); // Give the name of your Database
    const College = database.collection("students"); // Give the name of your Collection

    const query = {
      // Define the query for deleting data here
      Name: "Meet", // For example, delete students with the name "Robin"
    };

    // Perform CRUD Operations Here
    const result = await College.deleteMany(query);
    console.log("Database Connected");
    console.log(`${result.deletedCount} data is deleted.`);
  } finally {
    await client.close();
  }
}

run().catch(console.dir);


you can run this files with this command --- node mongoinsert.js

Comments