Collection.list_indexes() or index_information() returns no data in PyMongo

Hi,

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).

Thanks much !.

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'}}

Hi Jack,

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.

Thanks much for looking into this.

Sanity check: Can you see the indexes when you connect to your Atlas instance via Compass?

@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 …

I can see the indexes when I browse the collections in the console (web). I am not using the Compass though.

Okay, @Satya_Tanduri

Test cases:

  • 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:

  1. You can connect and read Atlas collections from your Python driver.
  2. 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.

Hi Jack,

Here is a quick update. I tried the following and it seems to work in PyMongo.

           tdb = myclient[db_name]
           tcol = db[format(coll_name)]
           index_cursor = tcol.list_indexes()
           print ("\nindex_cursor TYPE:", type(index_cursor))
           for index in index_cursor:
              print ("\n")
              print (index, "--", type(index))
              print ("index.keys():", index.keys())
              print ("NAME:", index["name"]) # index name
              print ("VERSION:", index["v"]) # index version

I got this code snippet from the web (https://kb.objectrocket.com/mongo-db/how-to-use-python-to-check-if-an-index-exists-for-a-mongodb-collection-440). Now I can see the following output:

index_cursor TYPE: <class 'pymongo.command_cursor.CommandCursor'>

SON([('v', 2), ('key', SON([('_id', 1)])), ('name', '_id_')]) -- <class 'bson.son.SON'>
index.keys(): ['v', 'key', 'name']
NAME: _id_
VERSION: 2

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.

Yes _id is always there.
Congratulations, have fun with your project.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.