Skip to content

Instantly share code, notes, and snippets.

@marcoberri
Last active February 9, 2016 11:03
Show Gist options
  • Save marcoberri/953f33a85b56e0d00261 to your computer and use it in GitHub Desktop.
Save marcoberri/953f33a85b56e0d00261 to your computer and use it in GitHub Desktop.
MongoDB delete all collections in db with exclusion colleciton list
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