Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements