python-3.x - 方法 createIndex() 不可在集合上调用
问题描述
在createIndex
他们说的文档中,db.collection.createIndex(keys, options)
我createIndex()
用下面的代码打电话。我的数据库articles
的名称是,集合的名称是bce
. 里面bce
已经有一个带有字段的文档article
。
class Stockage():
def __init__(self):
self.connexion()
def connexion(self):
client = pymongo.MongoClient("localhost", 27017)
self.bce = client.articles.bce
sql = Stockage()
sql.bce.createIndex({article : "text"})
我有以下错误:
Traceback (most recent call last):
File "<ipython-input-1132-fc8762d315d1>", line 1, in <module>
sql.bce.createIndex({article : "text"})
File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\collection.py", line 3321, in __call__
self.__name.split(".")[-1])
TypeError: 'Collection' object is not callable. If you meant to call the 'createIndex' method on a 'Collection' object it is failing because no such method exists.
是不是bce
收藏?
解决方案
这是因为在 Pymongo 中调用该方法create_index()
而不是在shellcreateIndex()
中命名该方法。mongo
mongo
与shell 对应的参数相比,它的参数格式也不同。它不接受文档/字典作为索引规范,而是接受元组列表。这是因为您无法保证 Python 中 dict 键的顺序。
所以正确的行应该是:
sql.bce.create_index([("article", pymongo.TEXT)])
Pymongo Collection页面中提供了更多详细信息。
推荐阅读
- python - Kivy 文本输入自动滚动到文本顶部
- laravel - Laravel 记录到自定义频道不考虑最低级别
- regex - 302重定向到带有加号的网址
- node.js - Azure 存储下载到 Blob 过早结束 Function App 调用
- php - 如何在本地开发环境中访问特定于域的路由?
- c - 我真的不知道怎么了
- database - 如何从 Apex URL 中查找数据库名称
- sql - 我可以在雪花中加入使用后使用 WHERE 子句吗?
- sql - Teradata 创建查询失败,错误 3706 预期在 ')' 和 '.' 之间。
- python - flask send_from_directory within a file representation