I want to Count() indexes for each collection which collection name start with specific name.
1 Answer
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);
});
}
});
24 Comments
Neha B
this i will get all collection indexes but i want count of that and for each collection ..
Neha B
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 ?
HoefMeistert
Then use the 2nd example and add a filter inside the foreach, which "tests" the collection name.
HoefMeistert
@NehaB added an xtra sample
Neha B
still i am struggling with the query.i want count of indexes for each collection ??help me out?
|