首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从MLab迭代pymongo游标

从MLab迭代pymongo游标
EN

Stack Overflow用户
提问于 2016-08-24 15:25:13
回答 3查看 775关注 0票数 1

为什么游标不会迭代?我确信应该有一个简单的解决方案。

我已经尝试了多个堆栈溢出答案和Mongodb https://docs.mongodb.com/getting-started/python/query/的文档

代码如下:

代码语言:javascript
复制
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

结果:

代码语言:javascript
复制
Collection(Database(MongoClient(host=['ds047124.mlab.com:47124'], document_class=dict, tz_aware=False, connect=True), u'politicians_from_theage'), u'posts')

然后光标将打印其位置:

代码语言:javascript
复制
# Define Cursor
my_cursor = collection.find()
print my_cursor 

结果:

代码语言:javascript
复制
<pymongo.cursor.Cursor object at 0x0000000003247518>

然后,尝试在游标上迭代会提供一个超时:

代码语言:javascript
复制
 # Perform query 
    cursor = db.posts.find()
    #Iterate the cursor and print the documents.
for document in cursor:
    print(document)  #No Luck

回溯错误或迭代:

代码语言:javascript
复制
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”上迭代,每一个都提供了服务器超时的回溯错误。如有任何帮助/见解,我们将不胜感激

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-08-24 19:14:53

找到答案后,我将重点放在游标上,而不是将对象从JSON加载到JSON列表中。

最终代码如下(删除URI)

代码语言:javascript
复制
import json
from datetime import date, timedelta
from pymongo import MongoClient
from bson import json_util

#Connect to Mongo Client 
client = MongoClient('mongodb://user:pword@ds047124.mlab.com:47124/politicians_from_theage')
db = client.politicians_from_theage #define database used
print db
# Define Collection
collection = db.posts
print collection # print Collection(Database(MongoClient(host=['ds047124.mlab.com:47124']...


cursor = collection.find()
print cursor

# Obtain json 
json_docs = []
for doc in cursor:
    json_doc = json.dumps(doc, default=json_util.default)
    json_docs.append(json_doc)
print json_docs #json result

# List Comprehension version
#json_docs = [json.dumps(doc, default=json_util.default) for doc in cursor]

#To get back from json again as string list
docs = [json.loads(j_doc, object_hook=json_util.object_hook) for j_doc in json_docs]
print docs

print 'kitty terminates program'
票数 0
EN

Stack Overflow用户

发布于 2016-08-24 15:30:25

这可能会对你有所帮助:

代码语言:javascript
复制
 # 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.

如果它起作用了,请告诉我。

票数 1
EN

Stack Overflow用户

发布于 2019-05-14 03:18:15

试试这个:

代码语言:javascript
复制
cursor = db.posts.find()
for document in list(cursor):
    print(document) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39116919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档