English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS Basic Tutorial

NodeJS Express.js

NodeJS Buffer & URL;

NodeJS MySql

NodeJS MongoDB

NodeJS File (FS)

Other NodeJS

Node.js Delete Collection in MongoDB

In this Node.js tutorial, we will learn to use db.collection to delete the Collection from the Node.js application. The remove() method, and give an example.

The following is a step-by-step guide on how to delete a collection from MongoDB in a Node.js application.

Start the MongoDB service. Run the following command to start the MongoDB service
sudo service mongod start
Get the basic URL of the MongoDB service. To understand the basic URL of the MongoDB service, a simple trick is to open the terminal and run the Mongo Shell.
Terminal - Mongo Shell
arjun@nodejs:~$ mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings: 
2017-10-29T18:15:36.110+0530 I STORAGE [initandlisten]
When the Mongo Shell starts, it echoes the basic URL of MongoDB.
mongodb://127.0.0.1:27017
Prepare the complete URL. Attach the name of the database you want to connect to (for example, newdb) to the basic URL.
mongodb://127.0.0.1:27017/newdb
Create a MongoClient.
var MongoClient = require('mongodb').MongoClient;
Connect to the MongoDB server from MongoClient with the help of URL.
MongoClient.connect(url, <callback_function>);
Once the attempt to establish a connection with MongoClient is completed, the callback function will receive an error and the db object as parameters.

If the connection is successful, the db object points to the database newdb.

Get a reference to the MongoDB collection.

db.collection(<collection_name>, <callback_function>);

Once the db object pointing to the specified MongoDB database is obtained, it can be used with the above statement to obtain a reference to the required collection.

Delete the MongoDB collection. The following is the syntax of the remove() method, which is used to delete a collection from MongoDB in Node.js.

collection.remove({}, callback_function)

Parameter description:

  • collection - Quote the MongoDB collection we need to delete

  • callback_function   -  After Node attempts to delete the specified collection and prepares the result, this Node.js callback function will be called. The callback function receives an error and a result object as parameters.

Example Node.js Program

 // Example: Delete the "users" collection in the newdb database
var url = "mongodb://localhost:27017/newdb 
 
// Create a client to mongodb
var MongoClient = require('mongodb').MongoClient; 
 
// Connect the client to the mongo service
MongoClient.connect(url, function(err, db) { 
    if (err) throw err; 
    // db points to newdb
    console.log("Switched to ",++"database" 
    // Get the reference to the collection
    db.collection("users", function(err, collection) { 
        // Handle errors (if any)
        if (err) throw err; 
        // Delete mongodb collection
        collection.remove({}, function(err, result){ 
            // Handle errors (if any)
            if (err) throw err; 
            console.log("Collection is deleted!");+result); 
            // Close the connection with db after completion
            db.close(); 
        }); 
    }); 
 });

Output Result

~$ node node-js-mongodb-delete-collection.js 
Switched to newdb database
Collection is deleted! {"n":0,"ok":1}

Conclusion:

In this Node.js MongoDB tutorial:  Node.js – Delete a Collection from MongoDBWe learned how to use the mongodb package to delete a collection from the MongoDB database used in a Node.js application. In the next tutorial – Inserting a Document into a MongoDB Collection with Node.js, we will learn how to insert one or more documents into a MongoDB collection.