Last active
February 9, 2016 11:03
-
-
Save marcoberri/953f33a85b56e0d00261 to your computer and use it in GitHub Desktop.
MongoDB delete all collections in db with exclusion colleciton list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deleteAllCollection(dbName,excludeCollection){ | |
var dbOne = db.getSisterDB(dbName); | |
for(var colName in dbOne.getCollectionNames()){ | |
var collectionName = dbOne.getCollectionNames()[colName]; | |
if(!collectionName || collectionName == "") | |
continue; | |
if(excludeCollection.indexOf(collectionName) > -1) | |
continue; | |
dbOne[collectionName].drop(); | |
}; | |
}; | |
deleteAllCollection("<dbName>",["<exclude_col_name_1>","<exclude_col_name_2>","<exclude_col_name_N>"]]); | |
//run with | |
// mongo --host <host_name> --port <port_name> DropCollectionInDB.js | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment