MongoDB drop every database

后端 未结 8 1022
清歌不尽
清歌不尽 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:45

    Save this to drop_all_dbs.js:

    var databases = db.getMongo().getDBNames()
    for(var i in databases){
        db = db.getMongo().getDB( databases[i] );
        if(db.getName() == "admin" || db.getName() == "local"){
            print("skipping db " + db.getName())
            continue
        }
        print( "dropping db " + db.getName() );
        db.dropDatabase();
    }
    

    Now you can execute:

    mongo drop_all_dbs.js
    

    and all databases (except for admin and local) will be dropped.

    This answer is a copy of ALoR's one, just fix drop of system dbs

提交回复
热议问题