🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Node.js Notes
Topic #67

MongoDB Drop Collection


Drop Collection

You can delete a table, or collection as it is called in MongoDB, by using the drop() method.

The drop() method takes a callback function containing the error object and the result parameter which returns true if the collection was dropped successfully, otherwise it returns false.

Example

let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;

let dbo = db.db("mydb");

dbo.collection("customers").drop(function(err, delOK) {

if (err) throw err;
    if (delOK) console.log("Collection
deleted");
    db.close();
  });
});

Save the code above in a file called "demo_drop.js" and run the file:

C:\Users\Your Name>node demo_drop.js

Which will give you this result:

Collection deleted

db.dropCollection

You can also use the dropCollection() method to delete a table (collection).

The dropCollection() method takes two parameters: the name of the collection and a callback function.

Example

let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;

let dbo = db.db("mydb");

dbo.dropCollection("customers", function(err, delOK) {

if (err) throw err;
    if (delOK) console.log("Collection
deleted");
    db.close();
  });
});

Save the code above in a file called "demo_dropcollection.js" and run the file:

C:\Users\Your Name>node demo_dropcollection.js

Which will give you this result:

Collection deleted

Want to go beyond the notes?

Join CodingNow 2.0's Node.js course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

MongoDB Drop Collection – FAQs

Quick answers about learning MongoDB Drop Collection in Node.js.

This free note from CodingNow 2.0 explains MongoDB Drop Collection in Node.js — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Node.js topic on CodingNow 2.0, including MongoDB Drop Collection, is 100% free with no signup required.
With focused practice, most students grasp MongoDB Drop Collection in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now