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.要删除关联的用户,请在要删除的数据库中运行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 abortoplog entry. MongoDB尝试中止primary上指定索引的正在进行的构建,如果成功,将创建一个关联的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条目。

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 4.4 and later, 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 allmongos instances before reading or writing to that database.在读取或写入数据库之前,对所有mongos实例使用flushRouterConfig命令。
  • For MongoDB 4.0 and earlier, you must:对于MongoDB 4.0及更早版本,您必须:

    1. Run the dropDatabase command on a mongos.mongos上运行dropDatabase命令。
    2. Connect to each shard's primary and verify that the namespace has been dropped. 连接到每个分片的primary并验证命名空间是否已被删除。If it has not, rerun the dropDatabase command again directly from the primary.如果没有,请直接从primary重新运行dropDatabase命令。
    3. Connect to a mongos, switch to the config database, and remove any reference to the removed namespace from the databases, collections, chunks, tags, and locks collections:连接到mongos,切换到config数据库,并从databasescollectionschunkstagslocks集合中删除对已删除命名空间的任何引用:

      use config
      db.collections.remove( { _id: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} )
      db.databases.remove( { _id: "DATABASE" }, {writeConcern: {w: 'majority' }} )
      db.chunks.remove( { ns: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} )
      db.tags.remove( { ns: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} )
      db.locks.remove( { _id: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} )

      Where DATABASE represents the namespace of the database you just dropped.其中,DATABASE表示刚刚删除的数据库的名称空间。

    4. Connect to the primary of each shard, and remove any reference to the removed namespace from the cache.databases, cache.collections, and cache.chunks.DATABASE.COLLECTION collections:连接到每个分片的primary,并从cache.databasescache.collectionscache.chunks.DATABASE.COLLECTION集合中删除对已删除命名空间的任何引用:

      db.getSiblingDB("config").cache.databases.remove({_id:"DATABASE"}, {writeConcern: {w: 'majority' }});
      db.getSiblingDB("config").cache.collections.remove({_id:/^DATABASE.*/}, {writeConcern: {w: 'majority' }});
      db.getSiblingDB("config").getCollectionNames().forEach(function(y) {
         if(y.indexOf("cache.chunks.DATABASE.") == 0)
          db.getSiblingDB("config").getCollection(y).drop()
       })

      Where DATABASE represents the namespace of the database you just dropped.其中,DATABASE表示刚刚删除的数据库的名称空间。

    5. Use the flushRouterConfig command on allmongos 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 Event 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 drops the temp database:mongosh中的以下示例使用use<database>操作将当前数据库切换到临时数据库,然后使用dbdropDatabase()方法删除temp数据库:

use temp
db.dropDatabase()
Tip提示
See also: 参阅:
←  db.currentOp()db.fsyncLock() →