python – 循环Pymongo游标在一些迭代后返回bson.errors.InvalidBSON错误

我正在尝试用pymongo进行简单的查询并循环结果.

这是我正在使用的代码:

data = []
tam = db.my_collection.find({'timestamp': {'$gte': start, '$lte':end}}).count()
for i,d in enumerate(table.find({'timestamp': {'$gte': start, '$lte':end}}):
    print('%s of %s' % (i,tam))
    data.append(d)

start和end变量是datetime python对象.一切都运行良好,直到我得到以下输出:

2987 of 12848
2988 of 12848
2989 of 12848
2990 of 12848
2991 of 12848
2992 of 12848
Traceback (most recent call last):
  File "db_extraction\extract_data.py", line 68, in <module>
    data = extract_data(yesterday,days = 1)
  File "db_extraction\extract_data.py", line 24, in extract_data
    for i,d in enumerate(table.find({'timestamp': {'$gte': start, '$lte':end}}).limit(100000)):
  File "\venv\lib\site-packages\pymongo\cursor.py", line 1169, in next
    if len(self.__data) or self._refresh():
  File "\venv\lib\site-packages\pymongo\cursor.py", line 1106, in _refresh
    self.__send_message(g)
  File "\venv\lib\site-packages\pymongo\cursor.py", line 971, in __send_message
    codec_options=self.__codec_options)
  File "\venv\lib\site-packages\pymongo\cursor.py", line 1055, in _unpack_response
    return response.unpack_response(cursor_id, codec_options)
  File "\venv\lib\site-packages\pymongo\message.py", line 945, in unpack_response
    return bson.decode_all(self.documents, codec_options)
bson.errors.InvalidBSON

我尝试过的第一件事就是更改查询的范围以检查它是否与数据相关,而事实并非如此.另一个范围在6360的1615处停止并且出现相同的错误.

我也试过list(table.find({‘timestamp’:{‘$gte’:start,’$lte’:end}})和同样的错误.

另一个可能相关的信息是第一次查询真的很快.它会在返回错误之前将最后一个数字冻结一段时间.

所以我需要一些帮助.我在这里遇到限制吗?或者关于最新情况的任何线索?

这可能与this 2013 question有关,但作者说他没有输出错误.

谢谢!

编辑:

首先,感谢大家的时间和建议.不幸的是,我测试了所有的sugestions,我在同一个地方得到了同样的错误.我使用mongo shell打印了有问题的文件,它几乎和其他所有文件一样.
我改变了查询的范围,并尝试了其他日子.在所有的日子里都一样的问题,直到我发现一个随机运行给了我一个MEMORY ERROR.

1737 of 8011
1738 of 8011
1739 of 8011
1740 of 8011
1741 of 8011
Traceback (most recent call last):
  File "db_extraction\pymongo_test.py", line 14, in <module>
    for post in all_posts:
  File "\python_modules\venv\lib\site-packages\pymongo\cursor.py", line 1189, in next
    if len(self.__data) or self._refresh():
  File "\python_modules\venv\lib\site-packages\pymongo\cursor.py", line 1126, in _refresh
    self.__send_message(g)
  File "\python_modules\venv\lib\site-packages\pymongo\cursor.py", line 931, in __send_message
    operation, exhaust=self.__exhaust, address=self.__address)
  File "\python_modules\venv\lib\site-packages\pymongo\mongo_client.py", line 1145, in _send_message_with_response
    exhaust)
  File "\python_modules\venv\lib\site-packages\pymongo\mongo_client.py", line 1156, in _reset_on_error
    return func(*args, **kwargs)
  File "\python_modules\venv\lib\site-packages\pymongo\server.py", line 106, in send_message_with_response
    reply = sock_info.receive_message(request_id)
  File "\python_modules\venv\lib\site-packages\pymongo\pool.py", line 612, in receive_message
    self._raise_connection_failure(error)
  File "\python_modules\venv\lib\site-packages\pymongo\pool.py", line 745, in _raise_connection_failure
    raise error
  File "\python_modules\venv\lib\site-packages\pymongo\pool.py", line 610, in receive_message
    self.max_message_size)
  File "\python_modules\venv\lib\site-packages\pymongo\network.py", line 191, in receive_message
    data = _receive_data_on_socket(sock, length - 16)
  File "\python_modules\venv\lib\site-packages\pymongo\network.py", line 227, in _receive_data_on_socket
    buf = bytearray(length)
MemoryError

这是间歇性的.我没有改变任何东西再次运行并得到旧的invalidBSON错误,并再次运行并得到内存错误.

我启动了任务管理器并再次运行,内存确实快速增长到95%的使用率并挂起.查询应该检索8GB RAM机器中的1GB数据,所以…我不知道这是否会发生.无论如何,使用pymongo从mongoDB检索数据并写入文件而不将所有内容都放入内存的代码建议可能会完成这项工作.奖金将是如果有人能解释为什么我得到一个无效的BSON而不是MemoryError(对于绝大多数的运行)在我的情况下.

谢谢

解决方法:

您的代码在我的计算机上正常运行.由于它适用于您的前2992条记录,我认为这些文档可能存在一些不一致.您的集合中的每个文档都遵循相同的架构和格式吗?你的pymongo更新了吗?

如果你想遍历每条记录,这是我的建议:

data = []
all_posts = db.my_collection.find({'timestamp': {'$gte': start, '$lte':end}})
tam = all_posts.count()
i = 0
for post in all_posts:
    i += 1
    print('%s of %s' % (i,tam))
    data.append(post)

问候,

上一篇:python – mongomock如何与马达一起使用?


下一篇:数据存储之非关系型数据库存储----MongoDB存储