Syntax
dropThe
dropcommand removes an entire collection from a database. The command has following syntax:Tip
In the
mongoShell, this command can also be run through thedrop()helper method.Helper methods are convenient for
mongousers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.{ drop: <collection_name>, writeConcern: <document>, comment: <any> } The command takes the following fields:
FieldDescriptiondropThe name of the collection to drop.
writeConcernOptional. A document expressing the write concern of the
dropcommand. Omit to use the default write concern.When issued on a sharded cluster,
mongosconverts the write concern of thedropcommand and its helperdb.collection.drop()to"majority".commentOptional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
mongod log messages, in the
attr.command.cursor.commentfield.Database profiler output, in the
command.commentfield.currentOpoutput, in thecommand.commentfield.
A comment can be any valid BSON type (string, integer, object, array, etc).
New in version 4.4.
The
mongoshell provides the equivalent helper methoddb.collection.drop().
Behavior
This command also removes any indexes associated with the dropped collection.
Starting in MongoDB 4.4, the
db.collection.drop()method anddropcommand abort any in-progress index builds on the target collection before dropping the collection. Prior to MongoDB 4.4, attempting to drop a collection with in-progress index builds results in an error, and the collection is not dropped.For replica sets or shard replica sets, aborting an index on the primary does not simultaneously abort secondary index builds. MongoDB attempts to abort the in-progress builds for the specified indexes on the primary and if successful creates an associated
abortoplog entry. Secondary members with replicated in-progress builds wait for a commit or abort oplog entry from the primary before either committing or aborting the index build.The
dropcommand and its helperdb.collection.drop()create an invalidate for any Change Streams opened on the dropped collection.Dropping a collection deletes its associated zone/tag ranges.
Resource Locking
Changed in version 4.2.
drop obtains an exclusive lock on the specified collection
for the duration of the operation. All subsequent operations on the
collection must wait until drop releases the
lock.
Prior to MongoDB 4.2, drop obtained an exclusive
lock on the parent database, blocking all operations on the
database and all its collections until the operation completed.