【问题标题】:How to use pymongo to process huge collections如何使用 pymongo 处理庞大的集合
【发布时间】:2014-03-19 14:04:54
【问题描述】:

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

这是我的代码:

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

【问题讨论】:

    标签: python mongodb cursor pymongo


    【解决方案1】:

    知道了!

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

    还是谢谢!

    【讨论】:

      【解决方案2】:

      你可以通过for语句查看集合

      for record in cursor:
        print record
      

      for 关键字实际上会为您调用 _iter_()next()http://docs.python.org/2/library/collections.html#collections.Iterator

      pymongo 中的

      Cursor 对象带有这两种方法。 https://github.com/mongodb/mongo-python-driver/blob/master/pymongo/cursor.py#L1010

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-31
        • 2020-06-05
        • 1970-01-01
        • 1970-01-01
        • 2014-09-04
        • 2011-05-30
        相关资源
        最近更新 更多