0

我在网上寻找我的问题,但有点卡住了,我正在尝试使用 pymongo 使用游标检查 MongoDB 上的一个巨大集合,但是它似乎没有像 JS 这样的 python 实现 hasNext() 。

这是我的代码:

cursor = news.find() 
while(cursor.hasNext()):    
     doc = cursor.next()
4

2 回答 2

1

知道了!

pymongo 没有 hasNext(),如果没有更多对象,则 next() 方法返回 None,所以这就是诀窍

不管怎样,谢谢!

于 2013-04-02T04:19:46.200 回答
1

您可以通过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

于 2014-03-19T06:04:54.670 回答