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: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描述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 thedrop
command and its helperdb.collection.drop()
to"majority"
.mongos
将drop
命令及其助手db.collection.drop()
的写入关注转换为"majority"
。Returns:返回值:当成功删除集合时为true
when successfully drops a collection.true
。当要删除的集合不存在时为false
when collection to drop does not exist.false
。
Behavior行为
Thedb.collection.drop()
method anddrop
command create an invalidate for any Change Streams opened on dropped collection.db.collection.drop()
方法和drop
命令为在丢弃的集合上打开的任何更改流创建一个无效。Starting in MongoDB 4.4, the从MongoDB 4.4开始,db.collection.drop()
method anddrop
command abort any in-progress index builds on the target collection before dropping the collection.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 associatedMongoDB尝试中止primary上指定索引的正在进行的构建,如果成功,则创建一个相关的abort
oplog entry.abort
oplog条目。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从MongoDB 5.0开始,如果您试图从drop
command and thedb.collection.drop()
method return an error if you try to drop a collection in the admin database or the config database from amongos
.mongos
中删除admin
数据库或config
数据库中的集合,drop
命令和db.collection.drop()
方法将返回错误。To drop these collections, connect to the config server and run the command there.要删除这些集合,请连接到config
服务器并在那里运行命令。WarningDropping 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从MongoDB 6.0开始,db.collection.drop()
method drops the specified collection and any internal collections related to encrypted fields.db.collection.drop()
方法会删除指定的集合以及与加密字段相关的任何内部集合。WarningThedb.collection.drop()
method's behavior differs from the driver'sdrop
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, 在分片集群上,如果创建的集合与MongoDB 5.0之前删除的集合同名,mongos
may forward operations to the wrong shard. 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 对于运行MongoDB 4.4或更早版本的分片集群,如果使用drop()
method and then create a new collection with the same name, you must either:drop()
方法,然后创建一个同名的新集合,则必须:
Flush the cached routing table on every使用mongos
usingflushRouterConfig
.flushRouterConfig
刷新每个mongos
上的缓存路由表。Use使用db.collection.remove()
to remove the existing documents and reuse the collection.db.collection.remove()
可以删除现有文档并重新使用集合。
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, 在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.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
写入关注删除集合
w: 1
Write Concerndb.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 } } )