MongoDB drop every database

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

    you can create a javascript loop that do the job and then execute it in the mongoconsole.

    var dbs = db.getMongo().getDBNames()
    for(var i in dbs){
        db = db.getMongo().getDB( dbs[i] );
        print( "dropping db " + db.getName() );
        db.dropDatabase();
    }
    

    save it to dropall.js and then execute:

    mongo dropall.js
    
    0 讨论(0)
  • 2021-01-29 20:09

    You can also do this with a simple mongo command:

    db.adminCommand("listDatabases").databases.forEach( function (d) {
        if (d.name != "local" && d.name != "admin"  && d.name != "apiomat"  && d.name != "config")
            db.getSiblingDB(d.name).dropDatabase();
     })
    
    0 讨论(0)
提交回复
热议问题