drop

On this page本页内容

drop

The drop command removes an entire collection from a database. drop命令从数据库中删除整个集合。The command has following syntax:该命令具有以下语法:

{ drop: <collection_name>, writeConcern: <document>, comment: <any> }

The command takes the following fields:该命令接受以下字段:

Field字段Description描述
dropThe name of the collection to drop.要删除的集合的名称。
writeConcern

Optional. 可选。A document expressing the write concern of the drop command. 表示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"

comment

Optional. 可选。A user-provided comment to attach to this command. 用户提供了附加到此命令的注释。Once set, this comment appears alongside records of this command in the following locations:设置后,此注释将与此命令的记录一起显示在以下位置:

A comment can be any valid BSON type(string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

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

mongosh provides the equivalent helper method db.collection.drop().提供了等效的助手方法db.collection.drop()

Behavior行为

  • Starting in MongoDB 5.0, the drop command and the db.collection.drop() method will raise an error if passed an unrecognized parameter. 从MongoDB 5.0开始,如果传递了无法识别的参数,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 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上指定索引的正在进行的构建,如果成功,将创建一个关联的abort日志条目。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条目。
  • The drop command and its helper db.collection.drop() create an invalidate Event for any Change Streams opened on the dropped collection.drop命令及其助手db.collection.drop()为已删除集合上打开的任何变更流创建一个invalidate事件
  • 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. 从MongoDB 5.0开始,如果您试图从mongos删除admin数据库config数据库中的集合,drop命令和db.collection.drop()方法将返回错误。To drop these collections, connect to the config server and run the command there.要删除这些集合,请连接到配置服务器并在那里运行命令。
    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中更改

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, 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之前,drop获得了父数据库的独占锁,在操作完成之前阻止对数据库及其所有集合的所有操作。

←  currentOpdropDatabase →