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描述
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 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.

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.

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

Starting in MongoDB 4.2, you can specify a write concern to the method. If you specify a write concern that requires acknowledgement from fewer than the majority, the method uses write concern "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".

For MongoDB 4.2 and previous, when executing the dropDatabase command, you must perform the following additional steps if you intend to create a new database with the same name as the dropped database.

Note

MongoDB 4.4 does not require these additional steps when dropping and recreating a database with the same name.在删除和重新创建同名数据库时,MongoDB 4.4不需要这些额外的步骤。

  • For MongoDB 4.2, you must either:对于MongoDB 4.2,您必须:
    • Restart all mongos instances and all mongod shard members (including the secondary members);
    • Use the flushRouterConfig command on all mongos instances and all mongod shard members (including the secondary members) before reading or writing to that database.
  • For MongoDB 4.0 and earlier, you must either:

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.

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.

Example示例

The following example in the mongo shell uses the use <database> operation to switch the current database to the temp database and then uses the db.dropDatabase() method to drops the temp database:

use temp
db.dropDatabase()

See also参阅

dropDatabase