python - pymongo 或 VERSION 中不存在 FIND 方法?

标签 python pymongo

<分区>

我正在尝试使用带有登台数据库的 pymongo 做一些简单的事情。所以像往常一样做第一件事

import pymongo
connection = pymongo.Connection(host = 'mongodb://username:password@alaki-staging.member0.mongolayer.com:37017,/dbname?safe=true&slaveOk=true&fsync=true&journal=true&ssl=true')

现在就

connection.find({})
> Traceback (most recent call last):
  File "D:\MypythonCode\test.py", line 7, in <module>
    connection.find({})
  File "C:\Python27\lib\site-packages\pymongo\database.py", line 769, in __call_
_
    self.__name, self.__connection.__class__.__name__))
TypeError: 'Database' object is not callable. If you meant to call the 'find' me
thod on a 'Connection' object it is failing because no such method exists.

C:\Python27>python.exe D:\MypythonCode\test.py > test.d
Traceback (most recent call last):
  File "D:\MypythonCode\test.py", line 7, in <module>
    connection.find({})
  File "C:\Python27\lib\site-packages\pymongo\database.py", line 769, in __call_
_
    self.__name, self.__connection.__class__.__name__))
TypeError: 'Database' object is not callable. If you meant to call the 'find' me
thod on a 'Connection' object it is failing because no such method exists.

甚至检查版本!?

connection.version()

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    connection.version()
  File "C:\Python27\lib\site-packages\pymongo\database.py", line 769, in __call_
_
    self.__name, self.__connection.__class__.__name__))
TypeError: 'Database' object is not callable. If you meant to call the 'version'
 method on a 'Connection' object it is failing because no such method exists.
Press any key to continue . . .

有什么建议吗?!

最佳答案

为什么要

connection.find({}) 

工作?

您在没有阅读有关 MongoDB 和 PyMongo 的基础知识的情况下就在猜测 API 方法。

find() 显然是集合的方法,而不是连接的方法。

row = connection.your_collection.find()

就是你什么。

请在修改和猜测如何使用 API 之前阅读文档。

PyMongo 文档中充满了示例。

关于python - pymongo 或 VERSION 中不存在 FIND 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12403057/

相关文章:

python - 在函数中调用函数 - Python

python - Pandas :删除重复但连续的行并将第一行保留在组中

python - 将方法添加到python中加载的类/模块

python - 如何从 Python 搜索 MongoDB ISODate 字段?

python - DBRefs 可以包含额外的字段吗?

python - PyMongo insert_one 异常/错误处理

python - 如何将 Asyncio 与 while 循环一起使用

python - 如何使用 PyMongo 删除 MongoDB 数据库?

python - 适用于 Windows 的 Mongodb 电机驱动程序

python - 如何允许用户在 Django 中更改自己的密码?