【问题标题】:How to identify MongoDB text index using PyMongo如何使用 PyMongo 识别 MongoDB 文本索引
【发布时间】:2018-04-25 13:16:56
【问题描述】:

使用 PyMongo,MongoDB 3.6,我可以列出 collectionlist_indexes 的索引,正如文档中所说的 here。是否可以通过查看给定索引的属性来知道给定索引是否属于文本类型?

检索到的文本索引是这样的:

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是否存在就足够了吗?

【问题讨论】:

    标签: mongodb pymongo


    【解决方案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'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-17
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 2011-08-20
      • 2014-10-21
      • 1970-01-01
      相关资源
      最近更新 更多