Mongodb:“数据库”对象上的“查找”方法失败,因为不存在这样的方法

Tro*_*roy 6 mongodb pymongo mongodb-query

你们能帮我解决这个问题吗?Client.list_databse_names()打印列表,但下一行抛出以下错误。

File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 3339,
in __call__self.__name)
TypeError: 'Collection' object is not callable.
If you meant to call the 'find' method on a 'Database' object it is failing
because no such method exists.
Run Code Online (Sandbox Code Playgroud)

这是代码

import pymongo

client =pymongo.MongoClient('mongodb+srv://server:password@cluster-sre.mongodb.net/test?retryWrites=true&w=majority')
print(client.list_database_names()) // outputs : ['data', 'admin', 'local']
results = client.locations.find({"city": "Bangalore"})
print(results) 
Run Code Online (Sandbox Code Playgroud)

还尝试使用“client.locations.findOne()”相同的错误消息。

Joh*_*yHK 4

对象的属性client是数据库,而不是集合。所以你的调用应该是这样的:

results = client.data.locations.find({"city": "Bangalore"})
Run Code Online (Sandbox Code Playgroud)

这假设data是集合所在的数据库locations