Node Js - Perform CRUD (Create, Retrieve, Update, Delete) operations on MongoDB Database

Node Js - Perform CRUD (Create, Retrieve, Update, Delete) operations on MongoDB Database


Insert one document in the Database


const mongodb = require("mongodb");

const MongoClient = mongodb.MongoClient;

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

const dbName = "demoProject";

MongoClient.connect(connectionURL, (error, client) => {
  if (error) {
    return console.log(error);
  }

  const db = client.db(dbName);

  db.collection("products").insertOne(
    {
      title: "A book",
      desc: "A great book to read",
    },
    (error, result) => {

      if(error){
        return console.log('Error in document creation.')
      }
      console.log(result.ops);
    }
  );

  console.log("Record created");
});



Note: result.ops in the above code logs the documents inserted in the database.




Insert multiple documents 



const mongodb = require("mongodb");

const MongoClient = mongodb.MongoClient;

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

const dbName = "demoProject";

MongoClient.connect(  connectionURL, { usenewUrlParser: true }, (error, client) => {
    if (error) {
      return console.log(error);
    }

    const db = client.db(dbName);

    db.collection("products").insertMany(
      [
        {
          title: "Node Book",
          desc: "A great book to read",
        },
        {
          title: "laptop",
          desc: "HP laptop",
        },
        {
          title: "Speaker",
          desc: "Amazing speaker",
        },
      ],

      (error, result) => {
        if (error) {
          return console.log("Error in document creation.");
        }
        console.log(result.ops);
      }
    );

    console.log("Record created");

    
  }
);




Update One Document



const mongodb = require("mongodb");

const ObjectID = mongodb.ObjectID;

const MongoClient = mongodb.MongoClient;

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

const dbName = "demoProject";

MongoClient.connect(
  connectionURL,
  { useUnifiedTopology: true },
  (error, client) => {
    if (error) {
      return console.log(error);
    }

    const db = client.db(dbName);

    db.collection("products").updateOne(
      {
        _id: new ObjectID("5e9f53dfc6a0792ba874aefb"),
      },
      {
        $set: {
          desc: "Updated desc",
        },
      },
      (error, result) => {
        if (error) {
          return console.log(error);
        }
        
      }
    );
  }
);


Update multiple documents

const mongodb = require("mongodb");

const ObjectID = mongodb.ObjectID;

const MongoClient = mongodb.MongoClient;

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

const dbName = "demoProject";

MongoClient.connect(
  connectionURL,
  { useUnifiedTopology: true },
  (error, client) => {
    if (error) {
      return console.log(error);
    }

    const db = client.db(dbName);

    db.collection("products").updateMany(
      {
        title:'A book',
      },
      {
        $set: {
          desc: "All books are awesome",
        },
      },
      (error, result) => {
        if (error) {
          return console.log(error);
        }
        
      }
    );
  }
);



Retrieve Documents

const mongodb = require("mongodb");

const ObjectID = mongodb.ObjectID;

const MongoClient = mongodb.MongoClient;

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

const dbName = "demoProject";

MongoClient.connect(
  connectionURL,
  { useUnifiedTopology: true },
  (error, client) => {
    if (error) {
      return console.log(error);
    }

    const db = client.db(dbName);

    db.collection("products").find(
      {
        title: 'A Book',
      }      
    ).toArray((error, result)=>{
      console.log(result)
    });
  }
);



Delete one Document

const mongodb = require("mongodb");

const ObjectID = mongodb.ObjectID;

const MongoClient = mongodb.MongoClient;

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

const dbName = "demoProject";

MongoClient.connect(
  connectionURL,
  { useUnifiedTopology: true },
  (error, client) => {
    if (error) {
      return console.log(error);
    }

    const db = client.db(dbName);

    db.collection("products").deleteOne(
      {
        _id: new ObjectID("5e9f53dfc6a0792ba874aefc"),
      },
      (error, result) => {
        if (error) {
          console.log(error);
        }
      }
    );
  }
);



Delete multiple documents



const mongodb = require("mongodb");

const ObjectID = mongodb.ObjectID;
const MongoClient = mongodb.MongoClient;
const connectionURL = "mongodb://localhost:27017";
const dbName = "demoProject";

MongoClient.connect(
  connectionURL,
  { useUnifiedTopology: true },
  (error, client) => {
    if (error) {
      return console.log(error);
    }

    const db = client.db(dbName);

    db.collection("products").deleteMany(
      {
        title:'A book'
      },
      (error, result) => {
        if (error) {
          console.log(error);
        }
      }
    );
  }
);



Post a Comment

0 Comments