Drop Collection Mongoose
Introduction
Working with documents is one prime task in MongoDB. But collections store these documents. Handling collections is also one important part in MongoDB. There are few methods provided by mongoose that can be used to work with collections. One of these methods is the dropCollection() method. This method is used to delete a collection from a database. In this article, we will discuss how to use dropCollection() method in mongoose.
dropCollection() method
Before we discuss how to drop a collection, we need a collection.
1 2 3 4 5 | > db.details.find() { "_id" : ObjectId("5d7a9edaf144032948dfe18c"), "__v" : 0 } { "_id" : ObjectId("5d7a9f6a70cc583f45efc65a"), "name" : "Sam", "age" : 23 } { "_id" : ObjectId("5d7a9f7270cc583f45efc65b"), "name" : "Lisa", "age" : 26 } > |
Here is the details collection that we will delete using dropCollection() method. The name of the collection is “details” and there are three documents in it.
Let’s go through the mongoose file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | const express = require("express"); const app = express(); const bodyParser = require("body-parser"); const cors = require("cors"); const mongoose = require("mongoose"); const PORT = 4000; const router = express.Router(); let detail = require("./details.model"); app.use(cors()); app.use( bodyParser.urlencoded({ extended: true }) ); mongoose.connect("mongodb://127.0.0.1:27017/details", { useNewUrlParser: true }); const connection = mongoose.connection; connection.once("open", function() { console.log("Mongodb database success"); }); router.route("/dropCollection").delete(function(req, res) { connection.dropCollection("details", function(err, result) { if (err) { res.send(err); } else { res.send(result); } }); }); app.use("/", router); app.listen(PORT, function() { console.log("Server is running port: " + PORT); }); |
In this file, we used a lot many things. But observe the following line.
1 | const connection = mongoose.connection; |
Here we have the instance of the connection with the database. After that, we have a route to drop a collection = ‘/dropCollection’. Whenever this URL is used, the following function will be invoked.
1 2 3 4 5 6 7 8 9 | router.route("/dropCollection").delete(function(req, res) { connection.dropCollection("details", function(err, result) { if (err) { res.send(err); } else { res.send(result); } }); }); |
Inside this function, we used the connection variable that we created earlier to invoke the dropCollection() method. The dropCollection() method has two arguments – the name of the collection and a callback function. The callback function, in turn, has two arguments – error and the returned result of this method. The dropCollection() method will return true if the collection is removed successfully.
Let’s check if our mongoose code works or not. Here, we will use the postman tool to check the output.
The returned value is true. This means the collection was deleted successfully. Now there is no collection named as “details” in the database. Let’s use the same URL once again.
As there is no collection with name “details”, the dropCollection() method returns an object containing an error.
Conclusion
The dropCollection() method is a simple method that is used to delete a collection from a database. We have to pass the name of the collection and the rest is done by the mongoose. If there will be no collection of the name specified, the dropCollection() method will return an error.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started