如何使用PyMongo识别MongoDB文本索引
有了PyMongo、MongoDB 3.6,我可以像文档中所说的那样,使用list_索引列出集合的索引。通过查看给定索引的属性,是否可以知道该索引是否为文本类型 检索到的文本索引如下所示:如何使用PyMongo识别MongoDB文本索引,mongodb,pymongo,Mongodb,Pymongo,有了PyMongo、MongoDB 3.6,我可以像文档中所说的那样,使用list_索引列出集合的索引。通过查看给定索引的属性,是否可以知道该索引是否为文本类型 检索到的文本索引如下所示: SON([('v', 2), ('key', SON([('_fts', 'text'), ('_ftsx', 1)])), ('name', 'full_text_index'), ('default_language','english'), ('language_override', 'languag
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)])
检查textIndexVersion是否存在就足够了吗?是的,可以从属性中知道索引。索引的键包含此信息。您可以将有关索引的子数据转换为字典,并进行检查
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'))