mongodb 如何使用 pymongo 创建索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33541290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create an index with pymongo
提问by Maximilian
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:
我想在我的 Mongo DB 中的特定字段启用文本搜索。我想在 python (-> pymongo) 中实现这个搜索。当我按照互联网上的说明进行操作时:
db.foo.ensure_index(('field_i_want_to_index', 'text'), name="search_index")
I get the following error message:
我收到以下错误消息:
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
Is there a different/better way to create an index in pymongo?
是否有不同/更好的方法在 pymongo 中创建索引?
回答by chridam
Use the create_index
method where you pass in the keys as an array and TEXT
as the index direction :
使用将create_index
键作为数组和TEXT
索引方向传递的方法:
collection.create_index([('field_i_want_to_index', pymongo.TEXT)], name='search_index', default_language='english')