如何使用 pymongo 创建索引 [英] How can I create an index with pymongo
本文介绍了如何使用 pymongo 创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的 Mongo 数据库中的特定字段中启用文本搜索.我想在 python (-> pymongo) 中实现这个搜索.当我按照互联网上的说明进行操作时:
I want to enable text-search at a specific field in my Mongo DB. I want to implement this search in python (-> pymongo). When I follow the instructions given in the internet:
db.foo.ensure_index(('field_i_want_to_index', 'text'), name="search_index")
我收到以下错误消息:
Traceback (most recent call last):
File "CVE_search.py", line 8, in <module>
db.foo.ensure_index(('field_i_want_to_index', 'text'), name="search_index")
File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 1599, in ensure_index
return self.create_index(key_or_list, cache_for, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 1466, in create_index
index_doc = helpers._index_document(keys)
File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line 100, in _index_document
for (key, value) in index_list:
ValueError: too many values to unpack
是否有不同/更好的方法在 pymongo 中创建索引?
Is there a different/better way to create an index in pymongo?
推荐答案
使用 create_index
方法,您可以将键作为数组和 TEXT
作为索引方向:
Use the create_index
method where you pass in the keys as an array and TEXT
as the index direction :
collection.create_index([('field_i_want_to_index', pymongo.TEXT)], name='search_index', default_language='english')
这篇关于如何使用 pymongo 创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文