How to delete a table from MongoDB database?


Use drop() to delete a table. Following is the syntax −

db.yourCollectionName.drop();

Let us first create a collection with documents −

> db.deleteTableDemo.insertOne({"Name":"Chris","Age":23});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ccfb705140b992277dae0e6")
}
> db.deleteTableDemo.insertOne({"Name":"Carol","Age":21});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ccfb70c140b992277dae0e7")
}
> db.deleteTableDemo.insertOne({"Name":"David","Age":24});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ccfb714140b992277dae0e8")
}

Following is the query to display all documents from a collection with the help of find() method −

> db.deleteTableDemo.find().pretty();

This will produce the following output −

{
   "_id" : ObjectId("5ccfb705140b992277dae0e6"),
   "Name" : "Chris",
   "Age" : 23
}
{
   "_id" : ObjectId("5ccfb70c140b992277dae0e7"),
   "Name" : "Carol",
   "Age" : 21
}
{
   "_id" : ObjectId("5ccfb714140b992277dae0e8"),
   "Name" : "David",
   "Age" : 24
}

Here is the query to delete a table from MongoDB database −

> db.deleteTableDemo.drop();

This will produce the following output −

True

Above, True is returned i.e. the table deleted successfully.

Updated on: 30-Jul-2019

343 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements