来自pymongo的带有光标的列表理解 [英] List comprehension with cursor from pymongo

查看:74
本文介绍了来自pymongo的带有光标的列表理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的pymongo代码:

Here is my pymongo code:

client = MongoClient('localhost', 27017)
db = client['somedb']
collection = db.somecollection   
return_obj = collection.find({"field1":"red"})

#First print statement
print([item['field1'] for item in return_obj])

#Second print statement
print([item['field1'] for item in return_obj])

第一个打印语句产生非空列表,而第二个打印语句产生空列表.好像我必须重置return_obj上的索引.

The first print statement produces non-empty list, while the second one produces empty list. As if I have to reset an index on return_obj.

有什么想法吗?

推荐答案

这是正确的行为,应该是这样.您的变量return_obj是mongoDB游标,它是python中的特殊类,如这里.使用一次后,光标将用尽".

This is the correct behaviour, this is how it is supposed to be. Your variable return_obj is mongoDB cursor, which is a special class in python as described here. After using it once, the cursor is "exhausted".

这篇关于来自pymongo的带有光标的列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆