gpt4 book ai didi

python-3.x - 方法 createIndex() 不可在集合上调用

转载 作者:可可西里 更新时间:2023-11-01 09:28:40 25 4
gpt4 key购买 nike

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() 而不是 createIndex(),因为它在 mongo shell 中被命名。

与对应的 mongo shell 相比,它的参数格式也不同。它不接受文档/字典作为索引规范,而是接受元组列表。这是因为您无法保证 Python 中字典键的顺序。

所以正确的行应该是:

sql.bce.create_index([("article", pymongo.TEXT)])

有关更多详细信息,请参阅 Pymongo Collection页面。

关于python-3.x - 方法 createIndex() 不可在集合上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53175729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com