This post is next in series of learning MongoDB. We have learned how we can create Collection. In this post we will learn how we can drop the collection.
In MongoDB the first thing we need to do to drop a table is to switch to the database which hosts the collection (table).
use database_name
This will switch the MongoDB control to the database. To drop a collection we need to issue command
db.Collection_name.drop()
If database is successfully deleted it will return "true" or it return "false"
The below example will create a database named "Dept". A Collection "deptdetails" will be created and we will add some document(Row) and finally we will delete the Collection.
> use dept
switched to db dept
> db.createCollection("deptdetails")
{ "ok" : 1 }
> db.deptdetails.insert({DeptName:"Sales",DeptCode:10})
> db.deptdetails.find().pretty()
{
"_id" : ObjectId("52e6da801a7cd793275359d3"),
"DeptName" : "Sales",
"DeptCode" : 10
}
> db.deptdetails.drop()
true
>
What is MongoDB?
How to get Started with MongoDB
How to create or drop Database in MongoDB?
How to create Collection (Table) in MongoDB?