なぜカーソルが反復しないのですか?私は確信していると確信しているは簡単な解決策になるはずです。MLabからpymongoカーソルを反復する
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
結果:次に
Collection(Database(MongoClient(host=['ds047124.mlab.com:47124'], document_class=dict, tz_aware=False, connect=True), u'politicians_from_theage'), u'posts')
カーソル
は、私は、複数のスタックオーバーフローの答えと https://docs.mongodb.com/getting-started/python/query/
コードは以下のとおりであるMongoDBのためのマニュアルを参照して試してみましたその場所を印刷します:
# 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'を反復しようとしましたが、それぞれはサーバタイムアウトのトレースバックエラーを提供します。
# Perform query
cursor = db.posts.find().toAray(function(err, result){
#Iterate the cursor and print the documents.
for document in result:
print(document);
}) //Will give you array of objects.
それが動作するかどうか、私に教えてください - : すべてのヘルプ/洞察力が大幅にこれはあなたを助けるかもしれ