Node.JS MongoDB Delete Records

📔 : NodeJS 🔗

Node.JS में MongoDB Database में Documents (records) को update करने के लिए deleteMany() और deleteOne() method का use किया जाता है। इस topic में आप यही सीखेंगे कि इन methods का use करके किस तरह से Documents को delete किया जाता है।

Node.JS MongoDB deleteOne

updateOne(condition, callback) method का use करके Collection में किसी single document / record को delete किया जाता है।यह method दो arguments accept करता है , जो कि इस प्रकार हैं।

  • whereObj अगर हमें condition के according किसी particular document को delete करना है , तो Object में column : value pass कर सकते हैं , by default empty object pass किया जाता है। और अगर कोई condition नहीं apply करते तो सबसे पहला document delete होगा।

  • callback यह callback function होता है , जिसमे हम success और error handle करते हैं।

Node.JS MongoDB Delete Document Example
Copy Fullscreen Close Fullscreen
const { MongoClient } = require('mongodb');
const uri = "mongodb+srv://dbUser:dbPassword@cluster0.2n7fz.mongodb.net/node_mongodb?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true ,  connectTimeoutMS: 30000 ,  keepAlive: 1});
client.connect(error => {
  if(error){
    console.log("MongoDB Connect Error", error.name, error.message);
  }else{
    const database = client.db("node_mongodb");
    const users = database.collection("users");
    users.deleteOne({name : "Rahul Kumar"}, (error, result) => {
      if(error){
        console.log("Error", error.name, error.message);
      }else{
        console.log(result);
      }
    })
  }
});
Output

अगर कोई error नहीं आयी तो Output में आपको हमेशा की तरह ही , एक Object हो return होता है :)। जिसमे दो properties मिलेगी।

C:\Users\HP\Desktop\workspace\nodejs>node app.js
{ acknowledged: true, deletedCount: 1 }
  • acknowledged: true इसका मतलब है , कि MongoDB driver को database में write operation के लिए server द्वारा acknowledge कर दिया गया है।

  • deletedCount:1 number of deleted records , जिन्हे collection से delete किया गया है।

Node.JS MongoDB deleteMany

Database में multiple documents / records एक साथ delete करने के लिए deleteMany(condition, callback) method का use किया जाता है।
See Example :

Copy Fullscreen Close Fullscreen
const { MongoClient } = require('mongodb');
const uri = "mongodb+srv://dbUser:dbPassword@cluster0.2n7fz.mongodb.net/node_mongodb?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true ,  connectTimeoutMS: 30000 ,  keepAlive: 1});
client.connect(error => {
  if(error){
    console.log("MongoDB Connect Error", error.name, error.message);
  }else{
    const database = client.db("node_mongodb");
    const users = database.collection("users");
    users.deleteMany({age : 24}, (error, result) => {
      if(error){
        console.log("Error", error.name, error.message);
      }else{
        console.log(result);
      }
    })
  }
});
Output

multiple documents / records delete करने पर , output same रहता है बस , deletedCount property की value delete हुए documents के records change होती है।

C:\Users\HP\Desktop\workspace\nodejs>node app.js
{ acknowledged: true, deletedCount: 1 }
Delete All Documents

अब अगर आपको किस collection में available सभी documents / records को एक साथ delete करना होतो आपको deletedMany() method में empty Object {} pass कर देना है।

collection.deleteMany({}, (error, result) => {
  if(error){
    console.log("Error", error.name, error.message);
  }else{
    console.log(result);
  }
})

तो कुछ इस तरह से Node.JS में MongoDB Database में Documents(Rows) को delete करते हैं , I Hope, आपको अच्छे से समझ आया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers