使用PyMongo,MongoDB 3.6,我可以列出collection
的{{1}}索引,文档说here。是否可以通过查看其属性来了解给定索引是否为text类型?
检索到的文本索引是这样的:
list_indexes
是否足以检查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)])
是否存在?
答案 0 :(得分:1)
是的,可以从属性中了解索引。索引的关键字有此信息。您可以从索引的SON数据转换为字典并检查它。
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'))