【发布时间】:2018-04-12 07:20:10
【问题描述】:
我知道 mongoDB 中的通配符文本索引可以在 shell 中使用:
db.collection.createIndex( { "$**": "text" } )
并在 pymongo 中创建索引:
db[COLLECTION].create_index(index_name, index)
我不知道如何在 pymongo 中创建通配符索引。有人可以帮忙吗?
【问题讨论】:
我知道 mongoDB 中的通配符文本索引可以在 shell 中使用:
db.collection.createIndex( { "$**": "text" } )
并在 pymongo 中创建索引:
db[COLLECTION].create_index(index_name, index)
我不知道如何在 pymongo 中创建通配符索引。有人可以帮忙吗?
【问题讨论】:
签名: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, :data:~pymongo.TEXT)。
要在所有字段上创建文本索引,我们只需 使用包含字段和方向的单个元组的列表作为参数:
import pymongo
db.collection.create_index([("$**", pymongo.TEXT)])
print(list(db.collection.list_indexes()))
【讨论】: