Docs HomeMongoDB Manual

db.dropDatabase()

On this page本页内容

Definition定义

db.dropDatabase(<writeConcern>)

Removes the current database, deleting the associated data files.删除当前数据库,删除关联的数据文件。

The db.dropDatabase() method takes an optional parameter:db.dropDatabase()方法采用一个可选参数:

Field字段Description描述
writeConcernOptional.可选的。A document expressing the write concern to use if greater than "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 acknowledgements than write concern "majority", the operation uses "majority". 在副本集上发出时,如果指定的写入关注导致的成员确认少于写入关注"majority",则操作将使用"majority"Otherwise, the specified write concern is used.否则,将使用指定的写入关注。
When issued on a sharded cluster, MongoDB converts the specified write concern to "majority".当在分片集群上发布时,MongoDB会将指定的写入关注转换为"majority"
See also Behavior. 请参见行为
New in version 4.2. 4.2版新增。

Behavior行为

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

Locks锁定

Starting in version 4.2.2, the operation takes an exclusive (X) database lock only.从版本4.2.2开始,该操作只获取独占(X)数据库锁。

In versions 3.6-4.2.1, the operation takes an exclusive (X) database lock while dropping the collections in the database but a global lock when dropping the now-empty database.在版本3.6-4.2.1中,该操作在删除数据库中的集合时使用独占(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索引

Starting in MongoDB 4.4, the db.dropDatabase() method and dropDatabase command abort any in-progress index builds on collections in the target database before dropping the database. 从MongoDB 4.4开始,在删除数据库之前,db.dropDatabase()方法和dropDatabase命令会中止目标数据库中基于集合构建的任何正在进行的索引。Aborting an index build has the same effect as dropping the built index. 中止索引生成与删除生成的索引具有相同的效果。Prior to MongoDB 4.4, attempting to drop a database that contains a collection with an in-progress index build results in an error, and the database 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 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上指定索引的正在进行的构建,如果成功,则创建一个相关的abort oplog条目。具有复制的正在进行的生成的Secondary成员等待来自主成员的提交或中止操作日志条目,然后再提交或中止索引生成。

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")。

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

If you specify a write concern that requires acknowledgement 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 follow these additional steps for using the dropDatabase command, specific to your version of MongoDB:如果您打算创建一个与已删除数据库同名的新数据库,则必须按照以下附加步骤使用dropDatabase命令,具体针对您的MongoDB版本:

  • For MongoDB 5.0 and later, you must:对于MongoDB 5.0及更高版本,您必须:

    1. Run the dropDatabase command on a mongos, no additional steps required.mongos上运行dropDatabase命令,无需其他步骤。
  • For MongoDB 4.4, you must:对于MongoDB 4.4,您必须:

    1. Run the dropDatabase command on a mongos.mongos上运行dropDatabase命令。
    2. Once the command successfully completes, run the dropDatabase command once more on a mongos.一旦命令成功完成,请在mongos上再次运行dropDatabase命令。
  • For MongoDB 4.2, you must:对于MongoDB 4.2,您必须:

    1. Run the dropDatabase command on a mongos.mongos上运行dropDatabase命令。
    2. Once the command successfully completes, run the dropDatabase command once more on a mongos.一旦命令成功完成,请在mongos上再次运行dropDatabase命令。
    3. Use the flushRouterConfig command on all mongos instances before reading or writing to that database.在读取或写入数据库之前,请在所有mongos实例上使用flushRouterConfig命令。

These steps ensure 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 for any Change Streams opened on the dropped database or opened on the collections in the dropped database.db.dropDatabase()方法和dropDatabase命令为在已删除的数据库上打开或在已删除数据库中的集合上打开的任何更改流创建invalidate

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()
Tip

See also: 另请参阅:

dropDatabase