dropDatabase

On this page本页内容

Definition定义

dropDatabase

The dropDatabase command drops the current database, deleting the associated data files.dropDatabase命令删除当前数据库,删除关联的数据文件。

The command has the following form:命令的格式如下:

{ dropDatabase: 1, writeConcern: <document>, comment: <any> }

The command takes the following optional field:该命令接受以下可选字段:

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.请参见行为

comment

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了附加到此命令的注释。设置后,此注释将与此命令的记录一起显示在以下位置:

A comment can be any valid BSON type(string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

New in version 4.4.在版本4.4中新增

mongosh also provides the helper method db.dropDatabase().还提供了助手方法db.dropDatabase()

Behavior行为

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.具有复制的正在进行的生成的Secondary成员在提交或中止索引生成之前,等待来自主成员的提交或中止oplog条目。

Replica Set and Sharded Clusters副本集和共享群集

Changed in version 4.4.在版本4.4中更改

Replica Sets复制集

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

If you specify a write concern that requires acknowledgement from fewer than the majority, the command uses write concern "majority".如果指定的写关注点需要少数人的确认,则命令使用写关注点"majority"

If you specify a write concern that requires acknowledgement from more than the majority, the command 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. 这些步骤确保所有集群节点刷新其元数据缓存,其中包括新数据库的primary分片的位置。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 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 dropDatabase command to drop the temp database:mongosh中的以下示例使用use <database>操作将当前数据库切换到temp数据库,然后使用dropDatabase命令删除temp数据库:

use temp
db.runCommand( { dropDatabase: 1 } )
Tip提示
See also: 参阅:
←  dropdropConnections →