如何用pymongo创建索引

时间:2015-11-05 09:50:24

标签: mongodb indexing pymongo fuzzy-search text-search

我想在Mongo DB中的特定字段启用文本搜索。我想在python( - > pymongo)中实现这个搜索。当我按照互联网上的说明进行操作时:

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中创建索引有不同/更好的方法吗?

1 个答案:

答案 0 :(得分:22)

使用 create_index 方法,将密钥作为数组传递,并将 TEXT 作为索引方向传递:

collection.create_index([('field_i_want_to_index', pymongo.TEXT)], name='search_index', default_language='english')