Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of the collection.getIndexes() shell command in pymongo?

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?

like image 421
blueberryfields Avatar asked Nov 03 '14 22:11

blueberryfields


People also ask

How to check index on collection in MongoDB?

You can find all the available indexes in a MongoDB collection by using the getIndexes method. This will return all the indexes in a specific collection. Result: The output contains the default _id index and the user-created index student name index.

How do you create an index in PyMongo?

Create an index for a MongoDB collection, and specify the order. When you use PyMongo to create an index, you can pair the index name with either a 1 or a -1 integer value to explicitly set the sort order to ascending or descending, respectively.


1 Answers

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)]}} 
like image 84
siame Avatar answered Sep 26 '22 23:09

siame