Docs HomeMongoDB Manual

drop

Definition定义

drop

The drop command removes an entire collection from a database.drop命令从数据库中删除整个集合。

Tip

In mongosh, this command can also be run through the drop() helper method.mongosh中,这个命令也可以通过drop()助手方法运行。

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. 助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。 In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要方便,或者需要额外的返回字段,请使用数据库命令。

Syntax语法

The command has following syntax:该命令具有以下语法:

db.runCommand(
{
drop: <collection_name>,
writeConcern: <document>,
comment: <any>
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Description描述
dropThe name of the collection to drop.要删除的集合的名称。
writeConcernOptional.可选的。A document expressing the write concern of the drop command. 表示drop命令的写入关注的文档。Omit to use the default write concern.忽略使用默认的写入关注。
When issued on a sharded cluster, mongos converts the write concern of the drop command and its helper db.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: 要附加到此命令的用户提供的注释。设置后,此注释将与此命令的记录一起显示在以下位置: A comment can be any valid BSON type (string, integer, object, array, etc).
New in version 4.4. 4.4版新增。

mongosh provides the equivalent helper method db.collection.drop().提供了等效的辅助方法db.collection.drop()

Behavior行为

  • Starting in MongoDB 5.0, the drop command and the db.collection.drop() method will raise an error if passed an unrecognized parameter. 从MongoDB 5.0开始,如果传递了无法识别的参数,drop命令和db.collection.drop()方法将引发错误。In MongoDB 4.4 and earlier, unrecognized parameters are silently ignored.在MongoDB 4.4及更早版本中,无法识别的参数被默默忽略。
  • This command also removes any indexes associated with the dropped collection.此命令还将删除与已删除集合关联的任何索引。
  • Starting in MongoDB 4.4, the db.collection.drop() method and drop command abort any in-progress index builds on the target collection before dropping the collection. 从MongoDB 4.4开始,db.collection.drop()方法和drop命令会在删除集合之前中止目标集合上正在进行的任何索引构建。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.在MongoDB 4.4之前,尝试删除正在进行索引构建的集合会导致错误,并且该集合不会被删除。

    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 abort oplog entry. MongoDB尝试中止primary上指定索引的正在进行的构建,如果成功,则创建一个相关的中止操作日志条目。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.具有复制的正在进行的生成的成员在提交或中止索引生成之前,将等待来自主的提交或中止oplog条目。

  • The drop command and its helper db.collection.drop() create an invalidate for any Change Streams opened on the dropped collection.drop命令及其助手db.collection.drop()为在已删除集合上打开的任何更改流创建一个invalidate
  • Dropping a collection deletes its associated zone/tag ranges.删除集合将删除其关联的区域/标记范围。
  • Starting in MongoDB 5.0, the drop command and the db.collection.drop() method return an error if you try to drop a collection in the admin database or the config database from a mongos. 从MongoDB 5.0开始,如果您试图从mongos中删除管理数据库或配置数据库中的集合,drop命令和db.collection.drop()方法将返回错误。To drop these collections, connect to the config server and run the command there.要删除这些集合,请连接到config服务器并在那里运行命令。

    Warning

    Dropping collections in the admin database or the config database can leave your cluster in an unusable state.

Resource Locking资源锁定

Changed in version 4.2.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.对集合的所有后续操作都必须等待,直到drop释放锁为止。

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.在MongoDB 4.2之前,drop获得了对父数据库的独占锁定,阻止了对数据库及其所有集合的所有操作,直到操作完成。