drop
On this page本页内容
Definition定义
drop
-
Thedrop
command removes an entire collection from a database.drop
命令从数据库中删除整个集合。TipIn在mongosh
, this command can also be run through thedrop()
helper method.mongosh
中,这个命令也可以通过drop()
助手方法运行。Helper methods are convenient for助手方法对mongosh
users, but they may not return the same level of information as database commands.mongosh
用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要方便,或者需要额外的返回字段,请使用数据库命令。
Syntax语法
The command has following syntax:该命令具有以下语法:
db.runCommand(
{
drop: <collection_name>,
writeConcern: <document>,
comment: <any>
}
)
Command Fields命令字段
The command takes the following fields:该命令包含以下字段:
drop | |
writeConcern | drop command. drop 命令的写入关注的文档。When issued on a sharded cluster, mongos converts the write concern of the drop command and its helper db.collection.drop() to "majority" . |
comment |
|
mongosh
provides the equivalent helper method 提供了等效的辅助方法db.collection.drop()
.db.collection.drop()
。
Behavior行为
Starting in MongoDB 5.0, the从MongoDB 5.0开始,如果传递了无法识别的参数,drop
command and thedb.collection.drop()
method will raise an error if passed an unrecognized parameter.drop
命令和db.collection.drop()
方法将引发错误。In MongoDB 4.4 and earlier, unrecognized parameters are silently ignored.在MongoDB 4.4及更早版本中,无法识别的参数被默默忽略。This command also removes any indexes associated with the dropped collection.此命令还将删除与已删除集合关联的任何索引。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上指定索引的正在进行的构建,如果成功,则创建一个相关的中止操作日志条目。Secondaryabort
oplog entry.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条目。Thedrop
command and its helperdb.collection.drop()
create an invalidate for any Change Streams opened on the dropped collection.drop
命令及其助手db.collection.drop()
为在已删除集合上打开的任何更改流创建一个invalidate。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
中删除管理数据库或配置数据库中的集合,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.
Resource Locking资源锁定
Changed in version 4.2.4.2版更改。
drop
obtains an exclusive lock on the specified collection for the duration of the operation. 在操作期间获取指定集合的独占锁。All subsequent operations on the collection must wait until 对集合的所有后续操作都必须等待,直到drop
releases the lock.drop
释放锁为止。
Prior to MongoDB 4.2, 在MongoDB 4.2之前,drop
obtained an exclusive lock on the parent database, blocking all operations on the database and all its collections until the operation completed.drop
获得了对父数据库的独占锁定,阻止了对数据库及其所有集合的所有操作,直到操作完成。