Warum wird der Cursor nicht iterieren? Ich bin sicher, dass eine einfache Lösung sein sollte.Iterate einen Pymongo-Cursor von MLab
Ich habe mehrere Stack-Überlauf Antworten versucht, und die Dokumentation für MongoDB https://docs.mongodb.com/getting-started/python/query/
Der Code nach unten:
from pymongo import MongoClient
#Connect to Mongo Client
client = MongoClient('mongodb://the_username:[email protected]:47124/politicians_from_theage')
db = client.politicians_from_theage #define database used
# Define Collection
collection = db.posts
print collection
Ergebnis:
Collection(Database(MongoClient(host=['ds047124.mlab.com:47124'], document_class=dict, tz_aware=False, connect=True), u'politicians_from_theage'), u'posts')
Dann wird der Cursor wird seinen Standort drucken:
# Define Cursor
my_cursor = collection.find()
print my_cursor
Ergebnis:
<pymongo.cursor.Cursor object at 0x0000000003247518>
dann über den Cursor, um zu versuchen und iterieren ein Timeout:
# Perform query
cursor = db.posts.find()
#Iterate the cursor and print the documents.
for document in cursor:
print(document) #No Luck
Traceback Fehler oder eine Iteration:
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
Ich habe versucht, auf 'cursor', 'my_cursor' und 'collection' zu iterieren, von denen jede einen Trace-Back-Fehler des Server-Timeouts liefert. Jede Hilfe/insight stark
geschätzt Sie würden