Created
May 26, 2014 12:28
-
-
Save Climax777/40eae9e4abe7ec94d4e3 to your computer and use it in GitHub Desktop.
MongoDB shell helper for dropping a collection while keeping the indexes
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
collectionsafedropper = function(database,col) { | |
var thedb = db.getSiblingDB(database); | |
var thecoll = thedb[col]; | |
var indexes = thecoll.getIndexes(); | |
print("Dropping: ", database.toString() + "." + col.toString()); | |
thecoll.drop(); | |
indexes.forEach(function(index) { | |
var key = index.key; | |
var options = {}; | |
if(index["sparse"] != null && index["sparse"] == true) { | |
// Seems that the shell uses an integer value for sparse | |
options.sparse=1; | |
} | |
if(index["unique"] != null && index["unique"] == true) { | |
// Seems that the shell uses a boolean value for unique | |
options.unique=true; | |
} | |
if(index["background"] != null && index["background"] == true) { | |
// Seems that the shell uses an integer value for background | |
options.background=1; | |
} | |
// TODO add more option checks here... | |
print("Inserting Key:", JSON.stringify(key),"Options:", JSON.stringify(options)); | |
thedb[col].ensureIndex(key,options); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment