MongoDB - Drop a Collection
To drop a collection in MongoDB, use the
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:
artists musicians producers
OK, so we'll delete the artists collection.
So the collection was dropped. We can take another quick look to see which collections we have now:
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:
It returned false because the collection doesn't exist.