Example drop collection in mongodb with nodejs use mongoose

Drop Collection

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

The dropCollection () 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: Delete the "customers" table

 

var mongoose = require('mongoose');

var db = mongoose.connection;

var Schema = mongoose.Schema;

db.on('error', console.error);

db.once('open', function () {

    console.log("db connect");

    db.dropCollection("customers", function (err, result) {

        if (err) {

            console.log("error delete collection");

        } else {

            console.log("delete collection success");

        }

    });

 

});

mongoose.connect('mongodb://192.168.1.43:27017/codewr');

 

Run and result:

 

C:\Nodejs>node mongooseDeleteCollection.js

db connect

delete collection success