0

I am new to python and I am using python 2.6. I wrote a script to fetch all collections from MongoDB databases, Now I am trying to find indexes also using the same script

Here's the code snippet which fetches collections from all DB's

import pymongo
import json

if __name__ == '__main__':
    client = pymongo.MongoClient("localhost", 27011, maxPoolSize=50)
    d = dict((db, [collection for collection in client[db].collection_names()])
             for db in client.database_names())
    print json.dumps(d)

and I want to integrate following MongoDB command into the script which will list all indexes present in DB

db.getCollectionNames().forEach(function(collection){indexes = db[collection].getIndexes();print(collection);printjson(indexes.length);indexes.forEach(function(item){print(item.name);});});

How can I achieve that in the script

Devendra
  • 185
  • 14
  • FYI. Linked duplicates courtesy of google search ["pymongo get collection names"](https://www.google.com/search?q=pymongo+get+collection+names) and ["pymongo list indexes"](https://www.google.com/search?q=pymongo+get+indexes). Top results from each. Please search before posting as most questions like this have been answered before. – Neil Lunn Mar 29 '19 at 08:23
  • My question is kind of different and I did my research before posting but could not get any solution My error is ***TypeError: 'Database' object is not callable. If you meant to call the 'index_information' method on a 'MongoClient' object it is failing because no such method exists. *** – Devendra Mar 29 '19 at 08:57
  • It's actually no different at all. That answer you are linked to has a link to the pymongo documentation for the method you are attempting to use. It clearly states which object you invoke the method from. As does the error message for that matter. I suggest you read both. – Neil Lunn Mar 29 '19 at 09:00

0 Answers0