【问题标题】:pymongo.errors.CursorNotFound: cursor id '…' not found at serverpymongo.errors.CursorNotFound:在服务器上找不到游标 id '...'
【发布时间】:2018-11-18 12:42:22
【问题描述】:

我正在尝试使用 pymongo 从 mongodb 读取大约 1M 文档到 csv 文件。我的代码如下:

import csv
from pymongo import MongoClient
from datetime import datetime
from bson import json_util
from tempfile import NamedTemporaryFile

client = MongoClient('mongodb://login:pass@server:port')
db = client.some_mongo_database
collection = db.some_mongo_collection

fromDate = datetime.strptime("2018-05-15 21:00", '%Y-%m-%d %H:%M')
tillDate = datetime.strptime("2018-05-16 21:00", '%Y-%m-%d %H:%M')
query = {
        "$or": [
                 {"LastUpdated": {"$gte": fromDate
                                , "$lt": tillDate}
                 },
                 {"$and": [
                            {"Created": {"$gte": fromDate
                                       , "$lt": tillDate}
                            },
                            {"LastUpdated": None}
                       ]
                  }
            ]
        }

cursor = collection.find(query, no_cursor_timeout=True)

如果我愿意的话:

for row in cursor:
    print(row)
cursor.close()

一切正常,我可以拿到所有文件。 但如果我这样做:

with NamedTemporaryFile("w", delete=False) as temp:
    csv_writer = csv.writer(temp, delimiter='\t', quotechar='\b', quoting=csv.QUOTE_MINIMAL)
    for row in cursor:
        csv_row = [ [[row['_id']], str(json.dumps(row,default=json_util.default))] ]
        csv_writer.writerows(csv_row)
cursor.close()

大约 2 分钟后,我收到了 20 万份文件:

Traceback (most recent call last):
  File "mongo_data_loader.py", line 25, in <module>
    for row in cursor:

  File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 1169, in next
    if len(self.__data) or self._refresh():
  File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 1106, in _refresh
    self.__send_message(g)
  File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 975, in __send_message
    helpers._check_command_response(first)
  File "/Library/Python/2.7/site-packages/pymongo/helpers.py", line 142, in _check_command_response
    raise CursorNotFound(errmsg, code, response)
pymongo.errors.CursorNotFound: cursor id 184972541202 not found

我做错了什么?

Python 2.7.10
pymongo 3.6.1
mongo db.version() 3.6.5

【问题讨论】:

标签: python mongodb pymongo


【解决方案1】:

作为我的临时解决方法:

processed = 0

while True:
    cursor = collection.find(query, no_cursor_timeout=True).skip(processed)

    try:
        for row in cursor:
            csv_row = [ [[row['_id']], str(json.dumps(row,default=json_util.default))] ]
            csv_writer.writerows(csv_row)
            processed += 1
        cursor.close()
        break
    except CursorNotFound:
        print("Lost cursor. Retry with skip")

但上述关于行为的问题仍然存在

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 2014-08-03
    • 1970-01-01
    • 2015-06-21
    • 2012-11-13
    • 1970-01-01
    • 2016-04-28
    • 2011-05-29
    • 2020-09-24
    相关资源
    最近更新 更多