MongoDB drop every database

后端 未结 8 1017
清歌不尽
清歌不尽 2021-01-29 19:30

I would like to know if there\'re a command to drop every databases from my MongoDB?

I know if I want to drop only one datatable, I just need to type the name of the dat

8条回答
  •  攒了一身酷
    2021-01-29 19:46

    Adding to @ALoR's answer, for convenience you can put the following in ~/.mongorc.js

    function dropDatabases(){
        var mongo = db.getMongo();
    
        var dbNames = mongo.getDBNames();
        for (var i = 0; i < dbNames.length; i++) {
            var db = mongo.getDB( dbNames[i] );
    
            print( "Dropping database " + db.getName() + "..." );
            db.dropDatabase();
        }
    }
    

    Then at the mongo shell you can simply do

    dropDatabases()
    

    From the docs:

    Mongo will read the .mongorc.js file from the home directory of the user invoking mongo. In the file, users can define variables, customize the mongo shell prompt, or update information that they would like updated every time they launch a shell.

提交回复
热议问题