AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

dropDatabase (データベースコマンド)

dropDatabase

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

このコマンドは、次の環境でホストされている配置で使用できます。

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

注意

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

このコマンドの構文は、次のとおりです。

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

コマンドは、次の任意フィールドがあります。

フィールド
説明

writeConcern

任意。 より大きい場合に使用する 書込み保証 ( write concern ) "majority"を表すドキュメント

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

"majority"のデフォルト/最小書込み保証 (write concern) を使用する場合は省略します。

レプリカセット上で発行した際、指定された書込み保証によってノードの承認応答が書込み保証"majority" よりも少なくなる場合には、この操作では"majority" が使用されます。それ以外の場合は、指定された書込み保証が使用されます。

シャーディングされたクラスターで発行されると、MongoDB は指定された書込み保証 (write concern) を "majority" に変換します。

See also Behavior.

comment

任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。

コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。

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

この操作では、排他(X)データベース ロックのみが使用されます。

このコマンドは、現在のデータベースに関連付けられているユーザーは削除しません。関連付けられているユーザーを削除するには、削除するデータベースでdropAllUsersFromDatabaseコマンドを実行します。

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.

レプリカセットまたはシャーディングされたレプリカセットの場合、プライマリでインデックスを中止しても、セカンダリでのインデックス構築は同時に中止されません。MongoDB は、指定されたインデックスの進行中の構築をプライマリで中止しようとし、成功した場合には関連する abort の oplog エントリを作成します。進行中の構築が複製されたセカンダリ ノードは、インデックス構築をコミットまたは中止する前に、プライマリからのコミットまたは中止の oplog エントリを待ちます。

レプリカセット

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

過半数よりも少ないユーザーからの承認を必要とする書込み保証 (write concern)を指定した場合、コマンドは書込み保証 (write concern "majority"を使用します。

過半数以上の承認を必要とする書き込み保証を指定した場合、コマンドは指定した書き込み保証が使用されます。

シャーディングされたクラスター

シャーディングされたクラスターで発行されると、MongoDB は指定された書込み保証 (write concern) を "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.

これにより、すべてのクラスター ノードがメタデータ キャッシュを更新します。これには、新しいデータベースのプライマリ シャードのロケーションが含まれます。 そうしないと、読み取りのデータが失われる可能性があり、正しいシャードにデータが書き込まれない可能性があります。 回復するには、手動で中断する必要があります。

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.

警告

管理データベースまたはコンフィギュレーションデータベースを削除 (drop) すると、クラスターが使用できない状態になる可能性があります。

The db.dropDatabase() method and dropDatabase create an invalidate Event for any MongoDB Change Streams 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 } )