简体   繁体   English

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

[英]How to create a wildcard text index in mongoDB using pymongo?

I know wildcard text index in mongoDB can be created in shell using:我知道可以使用以下命令在 shell 中创建 mongoDB 中的通配符文本索引:

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

And to create a index in pymongo:并在 pymongo 中创建索引:

db[COLLECTION].create_index(index_name, index)

I don't know how to create a wildcard index in pymongo.我不知道如何在 pymongo 中创建通配符索引。 Can anybody help?有人可以帮忙吗?

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

Docstring: Creates an index on this collection . Docstring:在此collection上创建索引

Takes either a single key or a list of (key, direction) pairs. 采用单个键或(键,方向)对的列表。 The key(s) must be an instance of :class: basestring (:class: str in python 3), and the direction(s) must be one of (:data: ~pymongo.ASCENDING , :data: ~pymongo.DESCENDING , :data: ~pymongo.GEO2D , :data: ~pymongo.GEOHAYSTACK , :data: ~pymongo.GEOSPHERE , :data: ~pymongo.HASHED , :data: ~pymongo.TEXT ). 键必须是:class: basestring的实例(python 3中的:class: str ),方向必须是(:data: ~pymongo.ASCENDING ,:data: ~pymongo.DESCENDING ,:data: ~pymongo.GEO2D ,:data: ~pymongo.GEOHAYSTACK ,:data: ~pymongo.GEOSPHERE ,:data: ~pymongo.HASHED ,:data: ~pymongo.TEXT )。

To create a text index on all fields, we just use a list of a single tuple containing field and direction as argument: 要在所有字段上创建文本索引,我们只使用一个包含字段和方向的元组列表作为参数:

import pymongo

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

print(list(db.collection.list_indexes()))

I am trying to crate index using pymongo and running below command db.coll.create_Index([("$**", pymongo.TEXT)] ,unique= True)我正在尝试使用 pymongo 创建索引并在命令 db.coll.create_Index([("$**", pymongo.TEXT)] ,unique= True) 下运行

But I am getting the error但我收到错误

TypeError: 'Collection' object is not callable.类型错误:“集合”对象不可调用。 If you meant to call the 'create_Index' method on a 'Collection' object it is failing because no such method exists.如果您打算在“Collection”对象上调用“create_Index”方法,则会失败,因为不存在此类方法。

How to solve it如何解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM