Docs HomeMongoDB Manual

db.collection.drop()

On this page本页内容

Definition定义

db.collection.drop(<options>)
Important

mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the drop command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

mongo shell v4.4

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命令的包装器。

db.collection.drop() has the form:具有以下形式:

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

db.collection.drop() takes an optional document with the following field:采用具有以下字段的可选文档:

Field字段Description描述
writeConcernOptional.可选的。A document expressing the write concern of the db.collection.drop() operation. 表示db.collection.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". 当在分片集群上发出时,mongosdrop命令及其助手db.collection.drop()写入关注转换为"majority"
Returns:返回值:
  • true when successfully drops a collection.当成功删除集合时为true
  • false when collection to drop does not exist.当要删除的集合不存在时为false

Behavior行为

  • The db.collection.drop() method and drop command create an invalidate for any Change Streams opened on dropped collection.db.collection.drop()方法和drop命令为在丢弃的集合上打开的任何更改流创建一个无效
  • 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上指定索引的正在进行的构建,如果成功,则创建一个相关的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.具有复制的正在进行的生成的secondary成员等待来自主成员的提交或中止操作日志条目,然后再提交或中止索引生成。

  • 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. 在分片集群上,如果创建的集合与MongoDB 5.0之前删除的集合同名,mongos可能会将操作转发到错误的分片。To avoid this situation use the version-specific instructions below:要避免这种情况,请使用以下特定版本的说明:

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

For a sharded cluster running MongoDB 4.4 or earlier, if you use the drop() method and then create a new collection with the same name, you must either:对于运行MongoDB 4.4或更早版本的分片集群,如果使用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资源锁定

Changed in version 4.2.4.2版更改。

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()释放锁。

Prior to MongoDB 4.2, db.collection.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之前,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. 以下操作将删除当前数据库中的students集合。The operation uses the 1 write concern:该操作使用1写入关注:

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