如何使用 pymongo 创建索引

How can I create an index with pymongo

我想在我的 Mongo 数据库中的特定字段启用文本搜索。我想在 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

有没有 different/better 在 pymongo 中创建索引的方法?

使用create_index method where you pass in the keys as an array and TEXT作为索引方向:

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