db.collection.drop()

On this page本页内容

Definition定义

db.collection.drop(<options>)

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Nodejs驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

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. 表示write concern of the 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"

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行为

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. db.collection.drop()在操作期间获取指定集合的独占锁。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: "majority" Write Concern使用w: "majority"写关注点删除一个集合

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 "majority" write concern:该操作使用"majority"写入关注点:

db.students.drop( { writeConcern: { w: "majority" } } )