Skip to content

Instantly share code, notes, and snippets.

@manojVivek
Last active December 26, 2016 12:05
Show Gist options
  • Save manojVivek/d5bb9d251262f4b00f4f1fa6574cf6a3 to your computer and use it in GitHub Desktop.
Save manojVivek/d5bb9d251262f4b00f4f1fa6574cf6a3 to your computer and use it in GitHub Desktop.
Lists the indexes that are not used(according to $indexstats) across all the databases.
var dbData = {}
db.adminCommand('listDatabases').databases.forEach(function(dBase){
if( "admin" == dBase.name || "local" == dBase.name){
return;
}
var currentDb = db.getSiblingDB(dBase.name)
var collData = {}
currentDb.getCollectionNames().forEach(function(collName){
unusedIndexes = currentDb.getCollection(collName).aggregate([{"$indexStats":{}},{"$match":{"accesses.ops" : 0,"name" : {"$ne" : "_id_"}}}]).toArray()
if(unusedIndexes.length > 0){
collData[collName] = unusedIndexes
}
})
if(Object.keys(collData).length > 0) {
dbData[dBase.name] = collData;
}
})
printjson(dbData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment