为什么游标不会迭代?我确信应该有一个简单的解决方案。
我已经尝试了多个堆栈溢出答案和Mongodb https://docs.mongodb.com/getting-started/python/query/的文档
代码如下:
from pymongo import MongoClient
#Connect to Mongo Client
client = MongoClient('mongodb://the_username:the_password@ds047124.mlab.com:47124/politicians_from_theage')
db = client.politicians_from_theage #define database used
# Define Collection
collection = db.posts
print collection
结果:
Collection(Database(MongoClient(host=['ds047124.mlab.com:47124'], document_class=dict, tz_aware=False, connect=True), u'politicians_from_theage'), u'posts')
然后光标将打印其位置:
# Define Cursor
my_cursor = collection.find()
print my_cursor
结果:
<pymongo.cursor.Cursor object at 0x0000000003247518>
然后,尝试在游标上迭代会提供一个超时:
# Perform query
cursor = db.posts.find()
#Iterate the cursor and print the documents.
for document in cursor:
print(document) #No Luck
回溯错误或迭代:
Traceback (most recent call last):
File "C:\PythonC\PythonWebScraping\17_MongoInterface\mongoget.py", line 18, in <module>
for result_object in my_cursor:
File "C:\Python27\lib\site-packages\pymongo\cursor.py", line 1090, in next
if len(self.__data) or self._refresh():
File "C:\Python27\lib\site-packages\pymongo\cursor.py", line 1012, in _refresh
self.__read_concern))
File "C:\Python27\lib\site-packages\pymongo\cursor.py", line 850, in __send_message
**kwargs)
File "C:\Python27\lib\site-packages\pymongo\mongo_client.py", line 827, in _send_message_with_response
server = topology.select_server(selector)
File "C:\Python27\lib\site-packages\pymongo\topology.py", line 210, in select_server
address))
File "C:\Python27\lib\site-packages\pymongo\topology.py", line 186, in select_servers
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: ds047124.mlab.com:47124: timed out
我尝试在“cursor”、“my_cursor”和“collection”上迭代,每一个都提供了服务器超时的回溯错误。如有任何帮助/见解,我们将不胜感激
https://stackoverflow.com/questions/39116919
复制相似问题