I am trying to retrieve information about indexes on collections in a database using PyMongo. My understanding is that PyMongo supports index_information() instead of getIndexes(). I tried using index_information() on a collection as follows:
indexes = tdb.tcol.index_information()
print(indexes).
I only see ‘{}’ as the output. I know there are indexes on collections in the database (Listingandreviews collection in the samples database). Any idea what I might be doing wrong or what is the correct way to display index information on collections (definitions of indexes basically).
You probably aren’t correctly authorized. Show a sanitized version of your connection string, e.g., did you put your authorization database in the URI? You can connect and not be able to see anything if you aren’t authorized.
$ python
Python 3.8.6
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymongo import MongoClient
>>> c = MongoClient('mongodb://foo:bar@localhost/admin')
>>> print(c.mydb.mycollection.index_information());
{'_id_': {'v': 2, 'key': [('_id', 1)], 'ns': 'mydb.mycollection'}, 'name_1': {'v': 2, 'unique': True, 'key': [('name', 1)], 'ns': 'mydb.mycollection'}}
I don’t think there is any issue with the connection string. I can query the collections/documents successfully. I am connecting to a Atlas service (free tier/4.4) from my PyMongo client (latest version).
print(myclient.tdb.tcol.index_information()) - produces the following o
{}
I can connect to the instance and query the db/collection and retrieve documents and information like avg document sizes etc. Maybe this is an issue with the Atlas Service.
@Satya_Tanduri I don’t think the problem is with Atlas. Below I get index_information() on my Atlas collection (names sanitized, but it works just fine).
>>> from pymongo import MongoClient
>>> c = MongoClient('mongodb+srv://myacct:xxxxxxxxxxxx@clusterN.abcde.mongodb.net/test?authSource=admin&replicaSet=ClusterN-shard-0&readPreference=primary')
>>> print(c.mydb.mycollection.index_information());
{'_id_': {'v': 2, 'key': [('_id', 1)], 'ns': 'mydb.mycollection'}}
I cannot of course be sure, but the first thing I would look for is some typographical error in your code.
As debugging steps, perhaps you can try to perform the same operation against the same Atlas colleciton with the driver for another language, e.g., PHP or JavaScript/Node.js …
index_information() on local collection using Python driver
index_information() on Atlas collection using another driver (PHP/JS/Java/etc.)
Really, the probability is very high that the problem is a typographical error or procedural error of some sort because:
You can connect and read Atlas collections from your Python driver.
Other people can execute index_information() on Atlas using the Python driver.
If you do an interactive Python session and show the same Atlas session both retrieving collection data and failing to retrieve index_information() and paste that session into this topic, it might help.
The id index is the default index I suppose. I see other indexes too now. This is all from the sample databases from MongoDB. Nothing custom there. Thanks much for your help.