- Reference >
mongo
Shell Methods >- Collection Methods >
- db.collection.drop()
db.collection.drop()¶
On this page
Definition¶
-
db.collection.
drop
(<options>)¶ Removes a collection or view from the database. The method also removes any indexes associated with the dropped collection. The method provides a wrapper around the
drop
command.Note
For a sharded cluster, if you use
db.collection.drop()
and then create a new collection with the same name, you must either:- Flush the cached routing table on every
mongos
usingflushRouterConfig
. - Use
db.collection.remove()
to remove the existing documents and reuse the collection. Use this approach to avoid flushing the cache.
db.collection.drop()
has the form:Changed in version 4.0:
db.collection.drop()
accepts an options document.db.collection.drop()
takes an optional document with the following field:Field Description writeConcern Optional. A document expressing the write concern of the
db.collection.drop()
operation. Omit to use the default write concern.When issued on a sharded cluster,
mongos
converts the write concern of thedrop
command and its helperdb.collection.drop()
to"majority"
.New in version 4.0.
Returns: true
when successfully drops a collection.false
when collection to drop does not exist.
- Flush the cached routing table on every
Behavior¶
- This method obtains a write lock on the affected database and will block other operations until it has completed.
- The
db.collection.drop()
method anddrop
command create an invalidate Event for any Change Streams opened on dropped collection. - Starting in MongoDB 4.0.2, dropping a collection deletes its associated zone/tag ranges.
Example¶
Drop a Collection Using Default Write Concern¶
The following operation drops the students
collection in the
current database.
Drop a Collection Using w: "majority"
Write Concern¶
Changed in version 4.0: db.collection.drop()
accepts an options document.
The following operation drops the students
collection in the
current database. The operation uses the "majority"
write concern: