Menu Docs

dropDatabase

dropDatabase

The dropDatabase command drops the current database, deleting the associated data files.

Esse comando está disponível em implantações hospedadas nos seguintes ambientes:

  • MongoDB Atlas: o serviço totalmente gerenciado para implantações do MongoDB na nuvem

Observação

Este comando é aceito em todos os clusters do MongoDB Atlas. Para obter informações sobre o suporte do Atlas a todos os comandos, consulte Comandos não suportados.

  • MongoDB Enterprise: a versão autogerenciada e baseada em assinatura do MongoDB

  • MongoDB Community: uma versão com código disponível, de uso gratuito e autogerenciada do MongoDB

O comando tem a seguinte sintaxe:

db.runCommand(
{
dropDatabase: 1,
writeConcern: <document>,
comment: <any>
}
)

O comando usa os seguintes campos opcionais:

Campo
Descrição

writeConcern

Optional. A document expressing the Escreva preocupação to use if greater than "majority"

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

Omit to use the default/minimum write concern of "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.

When issued on a sharded cluster, MongoDB converts the specified Escreva preocupação to "majority".

See also Comportamento.

comment

Opcional. Um comentário fornecido pelo usuário para anexar a este comando. Depois de definido, esse comentário aparece junto com os registros desse comando nos seguintes locais:

Um comentário pode ser qualquer tipo BSON válido (string, inteiro, objeto, array etc).

mongosh also provides the helper method db.dropDatabase().

The operation takes an exclusive (X) database lock only.

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.

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.

Para conjuntos de réplicas ou conjuntos de réplicas de estilhaços, abortar um índice no primário não anula simultaneamente as compilações de índice secundário. O MongoDB tenta abortar as compilações em andamento para os índices especificados no primário e, se for bem-sucedido, cria uma entrada de oplog abort associada. Os membros secundários com compilações replicadas em andamento aguardam uma entrada de oplog de confirmação ou abortamento do primário antes de confirmar ou abortar a compilação do índice.

Conjuntos de réplicas

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

If you specify a write concern that requires acknowledgment from fewer than the majority, the command uses write concern "majority".

If you specify a write concern that requires acknowledgment from more than the majority, the command uses the specified write concern.

Clusters fragmentados

When issued on a sharded cluster, MongoDB converts the specified Escreva preocupação to "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.

This ensures that all cluster nodes refresh their metadata cache, which includes the location of the fragmento primário 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 banco de dados admin or the config database from a mongos.

Aviso

Dropping the banco de dados admin or the config database can leave your cluster in an unusable state.

The db.dropDatabase() method and dropDatabase create an invalidate for any Fluxos de alterações opened on the dropped database or opened on the collections in the dropped database.

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:

use temp
db.runCommand( { dropDatabase: 1 } )

Dica

Veja também: