How to delete a table from MongoDB database?

MongoDBDatabaseBig Data Analytics

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.

raja
Published on 22-May-2019 08:26:20
Advertisements