Skip to content

Instantly share code, notes, and snippets.

@lisimia
Last active July 18, 2017 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lisimia/cef7cb4b1051f3342416e1243b28cf90 to your computer and use it in GitHub Desktop.
Save lisimia/cef7cb4b1051f3342416e1243b28cf90 to your computer and use it in GitHub Desktop.
`collection.index_information` behaves differently on pymongo2 and pymongo3

collection.index_information behaves differently on pymongo2 and pymongo3

pymongo 2.8 relied on the internal system table to gather the index information: https://github.com/mongodb/mongo-python-driver/blob/c284c51c8749cc3ab13f193a78b4113b8715dd51/pymongo/collection.py#L1286

pymongo 3.0+ relies on the admin command of "listindexes": https://docs.mongodb.com/manual/reference/command/listIndexes/ https://github.com/mongodb/mongo-python-driver/blob/53bd24bfc3bcb3c4d8b01f63e51ffd703d44b373/pymongo/collection.py#L1674

This command will return an error:

mongos> db.adminCommand({"listIndexes":"stuff2"})
{ "ok" : 0, "errmsg" : "no database", "code" : 26 }

Using the db.system.indexes table will not return an error:

mongos> db.system.indexes.find()
mongos>

In Pymongo2

In [5]: db = pymongo.MongoClient()['my_db']
+
In [6]: db.my_collection.index_information()
Out[6]: {}

In Pymongo3

In [27]: db.stuff.index_information()
---------------------------------------------------------------------------
OperationFailure                          Traceback (most recent call last)
<ipython-input-27-d9b8c8cafb2b> in <module>()
----> 1 db.stuff.index_information()

/usr/local/lib/python2.7/dist-packages/pymongo/collection.pyc in index_information(self)
   1715          u'x_1': {u'unique': True, u'key': [(u'x', 1)]}}
   1716         """
-> 1717         cursor = self.list_indexes()
   1718         info = {}
   1719         for index in cursor:

/usr/local/lib/python2.7/dist-packages/pymongo/collection.pyc in list_indexes(self)
   1675                 cursor = self._command(sock_info, cmd, slave_ok,
   1676                                        ReadPreference.PRIMARY,
-> 1677                                        codec_options)["cursor"]
   1678                 return CommandCursor(coll, cursor, sock_info.address)
   1679             else:

/usr/local/lib/python2.7/dist-packages/pymongo/collection.pyc in _command(self, sock_info, command, slave_ok, read_preference, codec_options, check, allowable_errors, read_concern, write_concern, parse_write_co
ncern_error, collation)
    230             write_concern=write_concern,
    231             parse_write_concern_error=parse_write_concern_error,
--> 232             collation=collation)
    233
    234     def __create(self, options, collation):

/usr/local/lib/python2.7/dist-packages/pymongo/pool.pyc in command(self, dbname, spec, slave_ok, read_preference, codec_options, check, allowable_errors, check_keys, read_concern, write_concern, parse_write_con
cern_error, collation)
    417                            read_concern,
    418                            parse_write_concern_error=parse_write_concern_error,
--> 419                            collation=collation)
    420         except OperationFailure:
    421             raise

/usr/local/lib/python2.7/dist-packages/pymongo/network.pyc in command(sock, dbname, spec, slave_ok, is_mongos, read_preference, codec_options, check, allowable_errors, address, check_keys, listeners, max_bson_s
ize, read_concern, parse_write_concern_error, collation)
    114             helpers._check_command_response(
    115                 response_doc, None, allowable_errors,
--> 116                 parse_write_concern_error=parse_write_concern_error)
    117     except Exception as exc:
    118         if publish:

/usr/local/lib/python2.7/dist-packages/pymongo/helpers.pyc in _check_command_response(response, msg, allowable_errors, parse_write_concern_error)
    208
    209             msg = msg or "%s"
--> 210             raise OperationFailure(msg % errmsg, code, response)
    211
    212

OperationFailure: no database
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment