如何使用pymongo在mongoDB中创建通配符文本索引?

时间:2018-04-12 07:20:10

标签: python mongodb pymongo

我知道mongoDB中的通配符文本索引可以使用以下命令在shell中创建:

db.collection.createIndex( { "$**": "text" } )

并在pymongo中创建一个索引:

db[COLLECTION].create_index(index_name, index)

我不知道如何在pymongo中创建通配符索引。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

  

签名: db.a.create_index(keys,** kwargs)

     

Docstring: Creates an index on this collection

     

采用单个键或(键,方向)对的列表。   密钥必须是以下的实例:class:basestring   (:class:str在python 3中),方向必须是其中之一   (:数据:~pymongo.ASCENDING,:数据:~pymongo.DESCENDING,   :data:~pymongo.GEO2D,:data:~pymongo.GEOHAYSTACK,   :data:~pymongo.GEOSPHERE,:data:~pymongo.HASHED,   :数据:~pymongo.TEXT

要在所有字段上创建文本索引,我们只是 使用包含字段和方向的单个元组的列表作为参数:

import pymongo

db.collection.create_index([("$**", pymongo.TEXT)])

print(list(db.collection.list_indexes()))
相关问题