db.collection.drop()

Definition

  • db.collection.drop()

mongo Shell Method

This page documents the mongo shell method, and doesnot refer to the MongoDB Node.js driver (or any other driver)method. For corresponding MongoDB driver API, refer to your specificMongoDB driver documentation instead.

Removes a collection or view from the database.The method also removes any indexes associated with the droppedcollection. The method provides a wrapper around thedrop command.

db.collection.drop() has the form:

Changed in version 4.0: db.collection.drop() accepts an options document.

  1. db.collection.drop( { writeConcern: <document> } )

db.collection.drop() takes an optional document with thefollowing field:

FieldDescriptionwriteConcernOptional. A document expressing the write concern of thedb.collection.drop() operation. Omit to use thedefault write concern.

When issued on a sharded cluster, mongos converts thewrite 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.

Behavior

Resource Locking

Changed in version 4.2.

db.collection.drop() obtains an exclusive lock on the specified collectionfor the duration of the operation. All subsequent operations on thecollection must wait until db.collection.drop() releases thelock.

Prior to MongoDB 4.2, db.collection.drop() obtained an exclusivelock on the parent database, blocking all operations on thedatabase and all its collections until the operation completed.

Example

Drop a Collection Using Default Write Concern

The following operation drops the students collection in thecurrent database.

  1. db.students.drop()

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 thecurrent database. The operation uses the "majority"write concern:

  1. db.students.drop( { writeConcern: { w: "majority" } } )