title: How do I check if an index is being used
body: <p>I have a mongodb replica set with a lot of databases, collections &amp; indexes.</p>
  
  <p>We did a lot of refactor and optimization and, of course, I have a lot of "creative queries" from the consumers.</p>
  
  <p>I would like to clean up the unused indexes. just wanna save some space.</p>
  
  <p>How can I check if an index is being used? I can afford to check index by index and drop the unused ones.</p>
  
  <p>Running an "explain" in all the possible queries is not an option :)</p>
  
  <p><strong>EDIT: SOLUTION BASED ON THE ACCEPTED ANSWER</strong></p>
  
  <p>The script was bugged. I am not a javascript expert, but I put the corrected script. I hope will be useful for someone:</p>
  
  <pre><code>DB.prototype.indexStats = function() {
    var queries = [];
    var collections = db.getCollectionNames();
  
    var findQuery = function(q) {
      for(entryIdx in queries) {
        if(q == queries[entryIdx].query) {
          return entryIdx;
        }
      }
      return -1;
    }
  
    for(cIdx in collections) {
      var cName = collections[cIdx];
      var nsName = db.getName()+"."+cName;
      if(cName.indexOf("system") == -1) {
        var i = 1;
        var count = db.system.profile.count({ns:nsName});
        print('scanning profile {ns:"'+nsName+'"} with '+count+' records... this could take a while...');
        db.system.profile.find({ns:nsName}).addOption(16).batchSize(10000).forEach(function(profileDoc) {           
          if(profileDoc.query &amp;&amp; !profileDoc.query["$explain"]) { 
            var qIdx = findQuery(profileDoc.query);
            if(qIdx == -1 &amp;&amp; profileDoc.query["query"] ) {
              var size = queries.push({query:profileDoc.query, count:1, index:""});                   
              var explain = db[cName].find(queries[size-1].query).explain();
              if(profileDoc.query &amp;&amp; profileDoc.query["query"]) {
                queries[size-1].sort = profileDoc.query["orderby"];
                if(queries[size-1].sort) {
                  explain = db[cName].find(queries[size-1].query.query).sort(queries[size-1].sort).explain();
                }
              }
              queries[size-1].cursor = explain.cursor;
              queries[size-1].millis = explain.millis;
              queries[size-1].nscanned = explain.nscanned;
              queries[size-1].n = explain.n;
              queries[size-1].scanAndOrder = explain.scanAndOrder ? true : false;
              if(explain.cursor &amp;&amp; explain.cursor != "BasicCursor") {
                queries[size-1].index = explain.cursor.split(" ")[1];             
              } else {
                print('warning, no index for query {ns:"'+nsName+'"}: ');
                printjson(profileDoc.query);
                print('... millis: ' + queries[size-1].millis);
                print('... nscanned/n: ' + queries[size-1].nscanned + '/' + queries[size-1].n);
                print('... scanAndOrder: ' + queries[size-1].scanAndOrder);
              }
            } else if ( qIdx != -1 ) {
              queries[qIdx].count++;
            }
          }
        });
      }
    }
  
    for(cIdx in collections) {
      var cName = collections[cIdx];
      if(cName.indexOf("system") == -1) {
        print('checking for unused indexes in: ' + cName);
        for(iIdx in db[cName].getIndexes()) {
          var iName = db[cName].getIndexes()[iIdx].name;
          if(iName.indexOf("system") == -1) {
            var stats = db[cName].stats();
            var found = false;
            for(qIdx in queries) {
              if(queries[qIdx].index == iName) {
                found = true;
                break;
              }
            }
            if(!found) {
              print('this index is not being used: ');
              printjson(iName);
            }
          }
        }
      }
    }
  }
  </code></pre>