What is the equivalent of the collection.getIndexe

2020-04-01 19:22发布

问题:

I can't seem to find a getIndexes() command implemented as part of pymongo's Collection object - is that intentional? is it implemented somewhere else in the class hierarchy?

if it isn't, what's the pymongo canonical way for getting the same effect?

回答1:

What you might be looking for is index_information() at the Collection level. From the docs:

Get information on this collection’s indexes.

Returns a dictionary where the keys are index names (as returned by create_index()) and the values are dictionaries containing information about each index.

>>> db.test.index_information()
{u'_id_': {u'key': [(u'_id', 1)]},
u'x_1': {u'unique': True, u'key': [(u'x', 1)]}}