MongoDB - Drop a Collection
To drop a collection in MongoDB, use the db.collection.drop()
method.
In MongoDB, the db.collection.drop()
method removes the collection from the database. If the collection exists, it will return true, if it doesn't exist, it will return false.
Drop a Collection that Exists
Here, we'll use db.collection.drop()
to drop a collection that exists.
First, let's quickly check to see which collections we have in our music database:
Results:
artists musicians producers
OK, so we'll delete the artists collection.
Resulting message:
true
So the collection was dropped. We can take another quick look to see which collections we have now:
Results:
musicians producers
Note that the db.collection.drop()
method doesn't accept any parameters. Just run it as specified above.
Attempt to Drop a Collection that doesn't Exist
So now that there's no longer an artists collection in our database, let's try to drop it and see what message we get:
Resulting message:
false
It returned false because the collection doesn't exist.