Reputation: 1625
With PyMongo, MongoDB 3.6, I can list the indexes for a collection
with list_indexes
as the documentation says here. Is it possible to know if a given index is of type text by looking at its attributes?
A text index retrieved is something like this:
SON([('v', 2), ('key', SON([('_fts', 'text'), ('_ftsx', 1)])), ('name',
'full_text_index'), ('default_language','english'),
('language_override', 'language'), ('ns', 'tiquetaque.employees'),
('weights', SON([('contract_data.department', 1), ('full_name', 1), ('nis',
1)])), ('textIndexVersion', 3)])
Is it enough to check if textIndexVersion
exists?
Upvotes: 0
Views: 85
Reputation: 38982
Yes, it is possible to know the index from the attributes. The key for an index has this information. You can convert from SON data about an index to a dictionary and check this.
def find_index_by_type(collection, type_):
indexes = (index.to_dict() for index in collection.list_indexes())
matches = [index for index in indexes if type_ in index['key'].values()]
return matches
# text indexes in collection named collection.
print(find_index_by_type(db.collection, 'text'))
# hashed indexes in collection named collection.
print(find_index_by_type(db.collection, 'hashed'))
Upvotes: 1