1

I want to Count() indexes for each collection which collection name start with specific name.

Neha B
  • 75
  • 3
  • 9

1 Answers1

4

Please check this part of the Mongo Documentation. Below a part of the documentation.

List all Indexes on a Collection

To return a list of all indexes on a collection, use the db.collection.getIndexes() method or a similar method for your driver.

For example, to view all indexes on the people collection:

db.people.getIndexes()

List all Indexes for a Database

To list all indexes on all collections in a database, you can use the following operation in the mongo shell:

db.getCollectionNames().forEach(function(collection) {
   indexes = db[collection].getIndexes();
   print("Indexes for " + collection + ":");
   printjson(indexes);
});

to limit, you could do something like:

db.getCollectionNames().forEach(function(collection) {
   if (collection.indexOf("%YOU SEARCH STRING HERE%") > -1){
      indexes = db[collection].getIndexes();
      print("Indexes for " + collection + ":");
      printjson(indexes);
   }
});

And now with count

db.getCollectionNames().forEach(function(collection) {
   if (collection.indexOf("%YOU SEARCH STRING HERE%") > -1){
      indexes = db[collection].getIndexes();
      print("Indexes for " + collection + ":");
      print(indexes.length);
   }
});

And with index names

db.getCollectionNames().forEach(function(collection) {
   if (collection.indexOf("valueblue") > -1){
      indexes = db[collection].getIndexes();
      print("Indexes for " + collection + ":");
      print(indexes.length);

      indexes.forEach(function(item){
        print(item.name);
      });
   }
});
HoefMeistert
  • 1,190
  • 8
  • 17