Definition定义
db.collection.drop(<options>)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该方法为dropcommand.drop命令提供了一个包装器。Returns:返回true
Note
If the specified collection doesn't exist, 如果指定的集合不存在,db.collection.drop() still returns true.db.collection.drop()仍将返回true。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.所有MongoDB Atlas集群都支持此命令。有关Atlas支持所有命令的信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
The drop() method has the following form:drop()方法的形式如下:
db.collection.drop( { writeConcern: <document> } )
The drop() method takes an optional document with the following field:drop()方法接受一个具有以下字段的可选文档:
writeConcern |
|
Behavior行为
Thedb.collection.drop()method anddropcommand create an invalidate Event for any Change Streams opened on dropped collection.db.collection.drop()方法和drop命令为在丢弃集合时打开的任何变更流创建一个无效事件。Thedb.collection.drop()method anddropcommand abort any in-progress index builds on the target collection before dropping the collection.db.collection.drop()方法和drop命令在删除目标集合之前中止任何正在进行的索引构建。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上指定索引的正在进行的构建,如果成功,则创建相关的abortoplog entry.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.在提交或中止索引构建之前,具有正在进行的复制构建的辅助成员会等待主成员的提交或中止oplog条目。Dropping a collection deletes its associated zone/tag ranges.删除集合会删除其关联的区域/标记范围。Starting in MongoDB 5.0, the从MongoDB 5.0开始,如果您尝试从dropcommand 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服务器并在那里运行命令。Warning
Dropping 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()方法会删除指定的集合和与加密字段相关的任何内部集合。Warning
Thedb.collection.drop()method's behavior differs from the driver'sdropmethod'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.驱动程序的连接必须启用自动加密,以便删除指定的集合和与加密字段相关的任何内部集合。mongoshalways 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. To avoid this situation use the version-specific instructions below:mongos可能会将操作转发到错误的分片。为避免这种情况,请使用以下特定版本的说明:
For a sharded cluster running MongoDB 5.0 or later, no special action is required. Use the 对于运行MongoDB 5.0或更高版本的分片集群,不需要执行任何特殊操作。使用drop() method and then create a new collection with the same name.drop()方法,然后创建一个同名的新集合。
For a sharded cluster, if you use the 对于分片集群,如果你使用drop() method and then create a new collection with the same name, you must either:drop()方法,然后创建一个同名的新集合,你必须:
Flush the cached routing table on every使用mongosusingflushRouterConfig.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资源锁定
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()释放锁。
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. The operation uses the 1 write concern:students集合。该操作使用1写入关注:
db.students.drop( { writeConcern: { w: 1 } } )