Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PyMongo识别MongoDB文本索引_Mongodb_Pymongo - Fatal编程技术网

如何使用PyMongo识别MongoDB文本索引

如何使用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

有了PyMongo、MongoDB 3.6,我可以像文档中所说的那样,使用list_索引列出集合的索引。通过查看给定索引的属性,是否可以知道该索引是否为文本类型

检索到的文本索引如下所示:

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'))