I want to Count() indexes for each collection which collection name start with specific name.
Asked
Active
Viewed 6,508 times
1 Answers
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
-
this i will get all collection indexes but i want count of that and for each collection .. – Neha B Oct 10 '16 at 06:32
-
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 ? – Neha B Oct 10 '16 at 06:53
-
Then use the 2nd example and add a filter inside the foreach, which "tests" the collection name. – HoefMeistert Oct 10 '16 at 06:59
-
@NehaB added an xtra sample – HoefMeistert Oct 10 '16 at 07:08
-
still i am struggling with the query.i want count of indexes for each collection ??help me out? – Neha B Oct 12 '16 at 12:21
-
@NehaB I added an extra sample which filters the collections, it will only do the count on the collection matching your search string. I don't know what problem you are facing now.. – HoefMeistert Oct 12 '16 at 12:30
-
i run that query but nothing is happing with that query.i m not getting any result. – Neha B Oct 12 '16 at 12:36
-
You need to replace %YOU SEARCH STRING HERE% with the collection value you want... ex. if (collection.indexOf("users") > -1){ will process only collections with users in the name. It is **case sensitive** – HoefMeistert Oct 12 '16 at 12:39
-
yes i already mention my collection name there but i m not getting any result – Neha B Oct 12 '16 at 12:44
-
yes it worked..now i want count for each collection ? – Neha B Oct 12 '16 at 12:46
-
its printing all indexes instead of print indexes i want count for that indexes . eg. how many indexes are there in these collections?? – Neha B Oct 12 '16 at 12:49
-
Updated my answer – HoefMeistert Oct 12 '16 at 13:03
-
thanku so much for your help..one more thing i can take CSV also for this ? – Neha B Oct 12 '16 at 13:17
-
@NehaB Then you have to script your csv processing – HoefMeistert Oct 12 '16 at 13:22
-
can u help me for this – Neha B Oct 12 '16 at 13:23
-
Dunno depends on your OS, but could try ;-) – HoefMeistert Oct 13 '16 at 06:40
-
can we count documents for each collection in this case only ?? – Neha B Oct 13 '16 at 10:03
-
Taking CSV is the only problem? – Neha B Oct 13 '16 at 10:55
-
Check out this [Stackoverflow Question](http://stackoverflow.com/questions/1560393/bash-shell-scripting-csv-parsing) for some samples, insert your script and youre good to go! – HoefMeistert Oct 13 '16 at 12:57
-
How can i print Index Name also..? – Neha B Nov 02 '16 at 10:00
-
want to print "ns" name also..with count..? help me out? – Neha B Nov 02 '16 at 10:00
-
Added a sample in my answer – HoefMeistert Nov 02 '16 at 10:39
-
thank you so much..may i know u know about python ,pandas?? – Neha B Nov 02 '16 at 10:49
-
Help me..If i want to match the condition and unset one parameter from my collection? How can i do?help me – Neha B Nov 23 '16 at 08:17