db.collection.drop()

On this page本页内容

Definition定义

db.collection.drop(<options>)
Important重要
mongosh Method

This is a mongosh method. This is not the documentation for Node.js or other programming language specific driver methods.

In most cases, mongosh methods work the same way as the legacy mongo shell methods. However, some legacy methods are unavailable in mongosh.

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

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

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

Note注意

For a sharded cluster, if you use db.collection.drop() and then create a new collection with the same name, you must either:对于分片集群,如果使用db.collection.drop(),然后创建同名的新集合,则必须执行以下操作之一:

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

Changed in version 4.0.在版本4.0中更改

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

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

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

Field字段Description描述
writeConcern

Optional. 可选。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".当在分片集群上发出时,mongos会将drop命令及其助手db.collection.drop()写入关注点转换为"majority"

New in version 4.0.在版本4.0中新增

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 Event for any Change Streams opened on dropped collection.db.collection.drop()方法和drop命令为在删除的集合上打开的任何更改流创建一个invalidate事件
  • 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 abortoplog 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成员在提交或中止索引生成之前,会等待来自主版本的提交或中止oplog条目。
  • Starting in MongoDB 4.0.2, dropping a collection deletes its associated zone/tag ranges.从MongoDB 4.0.2开始,删除集合会删除其关联的区域/标记范围。
  • 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. 从MongoDB5.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数据库中的集合可能会使集群处于不可用状态。

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写入关注点删除集合

Changed in version 4.0.在版本4.0中更改

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 } } )
←  db.collection.distinct()db.collection.dropIndex() →