Last active
December 26, 2016 12:05
-
-
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.
This file contains 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
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