1

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

0

1 Answer 1

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);
      });
   }
});
Sign up to request clarification or add additional context in comments.

24 Comments

this i will get all collection indexes but i want count of that and for each collection ..
i have 1000 of collections and i want to get count of those collection indexes which start with specific name.So here i need to use ; ----regex for specific collection name ----Indexes Count -----separately count of indexes for those collections. need help ? need to write some Script or what ?
Then use the 2nd example and add a filter inside the foreach, which "tests" the collection name.
@NehaB added an xtra sample
still i am struggling with the query.i want count of indexes for each collection ??help me out?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.