Database Manual / Reference / mongosh Methods / Databases

db.dropDatabase() (mongosh method方法)

Definition定义

db.dropDatabase(<writeConcern>)
Removes the current database, deleting the associated data files.删除当前数据库,删除关联的数据文件。

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. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关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 db.dropDatabase() method takes an optional parameter:db.dropDatabase()方法接受一个可选参数:

Field字段Description描述
writeConcern

Optional. 可选。A document expressing the write concern to use if greater than "majority".如果超过"majority",则表示使用写入关注的文档。

{ w: <value>, j: <boolean>, wtimeout: <number> }

Omit to use the default/minimum write concern of "majority".省略使用默认/最小写入关注"majority"

When issued on a replica set, if the specified write concern results in fewer member acknowledgments than write concern "majority", the operation uses "majority". Otherwise, the specified write concern is used.在副本集上发布时,如果指定的写关注导致的成员确认少于写关注"majority",则操作使用"majority"。否则,将使用指定的写入关注。

When issued on a sharded cluster, MongoDB converts the specified write concern to "majority".当在分片集群上发布时,MongoDB会将指定的写入关注转换为"majority"

See also Behavior.另请参见行为

Behavior行为

The db.dropDatabase() wraps the dropDatabase command.db.dropDatabase()封装dropDatabase命令。

Locks

The operation takes an exclusive (X) database lock only.该操作仅采用独占(X)数据库锁。

User Management用户管理

This command does not delete the users associated with the current database. 此命令不会删除与当前数据库关联的用户To drop the associated users, run the dropAllUsersFromDatabase command in the database you are deleting.要删除关联的用户,请在要删除的数据库中运行dropAllUsersFromDatabase命令。

Indexes索引

The db.dropDatabase() method and dropDatabase command abort any in-progress index builds on collections in the target database before dropping the database. Aborting an index build has the same effect as dropping the built index.db.dropDatabase()方法和dropDatabase命令在删除数据库之前,会中止目标数据库中基于集合的任何正在进行的索引构建。中止索引构建与删除构建的索引具有相同的效果。

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 abort oplog entry. 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.MongoDB尝试中止primary上指定索引的正在进行的构建,如果成功,则创建相关的abortoplog条目。在提交或中止索引构建之前,具有正在进行的复制构建的 Secondary成员会等待主成员的提交或中止oplog条目。

Replica Set and Sharded Clusters复制品集和分片集群

Replica Sets复制集

At minimum, db.dropDatabase() waits until all collection drops in the database have propagated to a majority of the replica set members (i.e. uses the write concern "majority").db.dropDatabase()至少会等待,直到数据库中的所有集合删除都传播到大多数副本集成员(即使用写关注"majority")。

You can specify a write concern to the method. If you specify a write concern that requires acknowledgment from fewer than the majority, the method uses write concern "majority".您可以为该方法指定写入关注。如果您指定了一个需要少于多数人确认的写关注,则该方法使用写关注"majority"

If you specify a write concern that requires acknowledgment from more than the majority, the method uses the specified write concern.如果您指定了一个需要超过大多数人确认的写入关注,则该方法将使用指定的写入关注。

Sharded Clusters分片集群

When issued on a sharded cluster, MongoDB converts the specified write concern to "majority".当在分片集群上发布时,MongoDB会将指定的写入关注转换为"majority"

If you intend to create a new database with the same name as the dropped database, you must run the dropDatabase command on a mongos.如果您打算创建一个与删除的数据库同名的新数据库,则必须在mongos上运行dropDatabase命令。

This ensures that all cluster nodes refresh their metadata cache, which includes the location of the primary shard for the new database. Otherwise, you may miss data on reads, and may not write data to the correct shard. To recover, you must manually intervene.这确保了所有集群节点刷新其元数据缓存,其中包括新数据库的主分片的位置。否则,您可能会在读取时丢失数据,并且可能无法将数据写入正确的分片。要恢复,您必须手动干预。

Starting in MongoDB 5.0, the dropDatabase command and the db.dropDatabase() method return an error if you try to drop the admin database or the config database from a mongos.从MongoDB 5.0开始,如果您尝试从mongos中删除admin数据库config数据库dropDatabase命令和db.dropDatabase()方法将返回错误。

Warning

Dropping the admin database or the config database can leave your cluster in an unusable state.删除admin数据库config数据库可能会使集群处于不可用状态。

Change Streams更改流

The db.dropDatabase() method and dropDatabase command create an invalidate Event for any Change Streams opened on the dropped database or opened on the collections in the dropped database.db.dropDatabase()方法和dropDatabase命令为在已删除的数据库上打开的或在已删除数据库中的集合上打开的任何更改流创建无效事件

Example示例

The following example in mongosh uses the use <database> operation to switch the current database to the temp database and then uses the db.dropDatabase() method to drop the temp database:mongosh中的以下示例使用use <database>操作将当前数据库切换到temp数据库,然后使用db.dropDatabase()方法删除临时数据库:

use temp
db.dropDatabase()