【问题标题】:Python Mongodb limiting by batch size times outPython Mongodb 批量大小限制超时
【发布时间】:2016-10-26 12:13:42
【问题描述】:

我正在尝试抓取一个巨大的 (5gb) mongo 数据库,因此我限制了批处理大小以便于管理。但是,我仍然收到超时错误:/

诚然,我的 mongo 知识不是最好的,所以如果我在做一些完全愚蠢的事情,请告诉我!我已经搜索了文档和其他问题,但没有一个答案有帮助。

这是我想要做的:

from pymongo import MongoClient

collection = MongoClient(host="mongodb://xxx@xxx")
cursor = collection.all_companies.companies
batch = cursor.find().batch_size(1).limit(1) # I tried w/ other numbers too

for item in batch:
    print item

这就是我得到的:

pymongo.errors.ServerSelectionTimeoutError: xxx:xxx: 超时

【问题讨论】:

    标签: python mongodb collections timeout pymongo


    【解决方案1】:

    要获得多个文档作为查询的结果,我们使用 find() 方法。 find() 返回一个 Cursor 实例,它允许我们遍历所有匹配的文档。

    About find()

    About Cursor

    connection = MongoClient(host="mongodb://xxx@xxx")
    collection = connection.all_companies.companies
    for item in collection.find():
        print item
    

    【讨论】:

    • 我不关心我得到多少物品,我只是不希望服务器超时。当我不限制批量大小时,它肯定会超时
    猜你喜欢
    • 2013-05-21
    • 1970-01-01
    • 2017-02-20
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 2015-08-23
    相关资源
    最近更新 更多