Database Manual / Reference / mongosh Methods / Collections

db.collection.drop() (mongosh method方法)

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.该方法为drop命令提供了一个包装器。

Returns:返回true

Note

If the specified collection doesn't exist, db.collection.drop() still returns true.如果指定的集合不存在,db.collection.drop()仍将返回true

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.所有MongoDB Atlas集群都支持此命令。有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The drop() method has the following form:drop()方法的形式如下:

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

The drop() method takes an optional document with the following field:drop()方法接受一个具有以下字段的可选文档:

Field字段Description描述
writeConcern

Optional. 可选。A document expressing the write concern of the db.collection.drop() operation. Omit to use the default write concern.表示db.collection.drop()操作的写入关注的文档。省略使用默认写入关注。

When issued on a sharded cluster, mongos converts the write concern of the drop command and its helper db.collection.drop() to "majority".当在分片集群上发出时,mongos会将drop命令及其助手db.collection.drop()写入关注转换为"majority"

Behavior行为

  • The db.collection.drop() method and drop command create an invalidate Event for any Change Streams opened on dropped collection.db.collection.drop()方法和drop命令为在丢弃集合时打开的任何变更流创建一个无效事件
  • The db.collection.drop() method and drop command abort any in-progress index builds on the target collection before dropping the collection.db.collection.drop()方法和drop命令在删除目标集合之前中止任何正在进行的索引构建。

    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上指定索引的正在进行的构建,如果成功,则创建相关的abortoplog条目。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条目。

  • 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中删除admin数据库config数据库中的集合,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.删除admin数据库config数据库中的集合可能会使集群处于不可用状态。

  • Starting in MongoDB 6.0, the db.collection.drop() method drops the specified collection and any internal collections related to encrypted fields.从MongoDB 6.0开始,db.collection.drop()方法会删除指定的集合和与加密字段相关的任何内部集合。

    Warning

    The db.collection.drop() method's behavior differs from the driver's drop method's behavior. db.collection.drop()方法的行为与驱动程序的drop方法的行为不同。The driver's connection must have automatic encryption enabled in order to drop both the specified collection and any internal collections related to encrypted fields. 驱动程序的连接必须启用自动加密,以便删除指定的集合和与加密字段相关的任何内部集合。mongosh always drops the specified collection and any internal collections related to encrypted fields.mongosh总是删除指定的集合和与加密字段相关的任何内部集合。

Reusing Dropped Collection Names on Sharded Clusters在分片集群上重复使用已删除的集合名称

On a sharded cluster, if you create a collection that has the same name as a previously deleted collection prior to MongoDB 5.0, mongos may forward operations to the wrong shard. To avoid this situation use the version-specific instructions below:在分片集群上,如果您创建的集合与MongoDB 5.0之前删除的集合同名,mongos可能会将操作转发到错误的分片。为避免这种情况,请使用以下特定版本的说明:

For a sharded cluster running MongoDB 5.0 or later, no special action is required. Use the drop() method and then create a new collection with the same name.对于运行MongoDB 5.0或更高版本的分片集群,不需要执行任何特殊操作。使用drop()方法,然后创建一个同名的新集合。

For a sharded cluster, if you use the drop() method and then create a new collection with the same name, you must either:对于分片集群,如果你使用drop()方法,然后创建一个同名的新集合,你必须:

Flushing the cached routing tables is the preferred procedure because it is faster than removing sharded collections with db.collection.remove(). 刷新缓存的路由表是首选过程,因为它比使用db.collection.remove()删除分片集合更快。Only use the remove() approach if you want to avoid flushing the cache.只有当你想避免刷新缓存时,才使用remove()方法。

Resource Locking资源锁定

db.collection.drop() obtains an exclusive lock on the specified collection for the duration of the operation. 在操作期间获得指定集合的独占锁。All subsequent operations on the collection must wait until db.collection.drop() releases the lock.对集合的所有后续操作都必须等待db.collection.drop()释放锁。

Example示例

Drop a Collection Using Default Write Concern使用默认写入关注删除集合

The following operation drops the students collection in the current database.以下操作将删除当前数据库中的students集合。

db.students.drop()

Drop a Collection Using w: 1 Write Concern使用w:1写关注删除集合

db.collection.drop() accepts an options document.接受选项文档。

The following operation drops the students collection in the current database. The operation uses the 1 write concern:以下操作将删除当前数据库中的students集合。该操作使用1写入关注:

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