我在网上寻找我的问题,但有点卡住了,我正在尝试使用 pymongo 使用游标检查 MongoDB 上的一个巨大集合,但是它似乎没有像 JS 这样的 python 实现 hasNext() 。
这是我的代码:
cursor = news.find()
while(cursor.hasNext()):
doc = cursor.next()
知道了!
pymongo 没有 hasNext(),如果没有更多对象,则 next() 方法返回 None,所以这就是诀窍
不管怎样,谢谢!
您可以通过for语句查看集合
for record in cursor:
print record
for关键字实际上为您调用了_ iter _()和next()。 http://docs.python.org/2/library/collections.html#collections.Iterator
pymongo 中的光标对象带有这两种方法。 https://github.com/mongodb/mongo-python-driver/blob/master/pymongo/cursor.py#L1010