pymongo - bson.errors.InvalidDocument 只是有时无缘无故地提出

Posted

技术标签:

【中文标题】pymongo - bson.errors.InvalidDocument 只是有时无缘无故地提出【英文标题】:pymongo - bson.errors.InvalidDocument raised only sometimes for no apparent reason 【发布时间】:2021-12-19 06:06:16 【问题描述】:

我的文档如下所示: "_id" : 5, "hunger" : 5, "energy" : 50

我正在调用这个函数..

def getEnergy(_id) -> int:
    record = db.systems.find("_id":_id) # systems is the collection
    return record[0]['energy']

并收到此错误..

  (...)
  File "C:\Users\mateo\AppData\Roaming\Python\Python39\site-packages\pymongo\cursor.py", line 692, in __getitem__
    for doc in clone:
  File "C:\Users\mateo\AppData\Roaming\Python\Python39\site-packages\pymongo\cursor.py", line 1238, in next
    if len(self.__data) or self._refresh():
  File "C:\Users\mateo\AppData\Roaming\Python\Python39\site-packages\pymongo\cursor.py", line 1155, in _refresh
    self.__send_message(q)
  File "C:\Users\mateo\AppData\Roaming\Python\Python39\site-packages\pymongo\cursor.py", line 1044, in __send_message
    response = client._run_operation(
  File "C:\Users\mateo\AppData\Roaming\Python\Python39\site-packages\pymongo\mongo_client.py", line 1424, in _run_operation
    return self._retryable_read(
  File "C:\Users\mateo\AppData\Roaming\Python\Python39\site-packages\pymongo\mongo_client.py", line 1525, in _retryable_read
    return func(session, server, sock_info, secondary_ok)
  File "C:\Users\mateo\AppData\Roaming\Python\Python39\site-packages\pymongo\mongo_client.py", line 1420, in _cmd
    return server.run_operation(
  File "C:\Users\mateo\AppData\Roaming\Python\Python39\site-packages\pymongo\server.py", line 98, in run_operation
    message = operation.get_message(
  File "C:\Users\mateo\AppData\Roaming\Python\Python39\site-packages\pymongo\message.py", line 351, in get_message
    request_id, msg, size, _ = _op_msg(
  File "C:\Users\mateo\AppData\Roaming\Python\Python39\site-packages\pymongo\message.py", line 743, in _op_msg
    return _op_msg_uncompressed(
bson.errors.InvalidDocument: cannot encode object: <pymongo.cursor.Cursor object at 0x0000021E52535670>, of type: <class 'pymongo.cursor.Cursor'>

有时该函数可以正常工作,有时它会引发错误。这似乎是服务器的问题,但我无法弄清楚究竟是什么问题。

【问题讨论】:

record 是一个光标。从find 返回的文档。如果 find 没有匹配且没有返回任何文档怎么办? @prasad_ 该文档始终存在于数据库中。 由于您是通过_id字段查询,所以您应该使用find_one。 find_one 返回一个 documentNone。所以,读完之后,检查None,然后使用文档。 @prasad_ 已注明。我仍然收到错误消息。此外,没有必要检查 None,因为文档始终存在。 【参考方案1】:

如果错误出现在该函数中(我们无法 100% 判断,因为您只发布了一半的堆栈跟踪),如果您将光标传递给函数,您将收到此错误;这个 sn-p 重现错误:

from pymongo import MongoClient

db = MongoClient()['mydatabase']

def getEnergy(_id) -> int:
    record = db.systems.find("_id":_id) # systems is the collection
    return record[0]['energy']

foo = db.somecollection.find()
getEnergy(foo)

错误:

bson.errors.InvalidDocument: cannot encode object: <pymongo.cursor.Cursor object at 0x0000019DB0A0B070>, of type: <class 'pymongo.cursor.Cursor'>

您需要检查您在哪里调用getExport() 并检查您传入的参数不是游标对象。

【讨论】:

以上是关于pymongo - bson.errors.InvalidDocument 只是有时无缘无故地提出的主要内容,如果未能解决你的问题,请参考以下文章

pymongo.Connection 用法详解

import pymongo exceptions.ImportError: No module named pymongo

pymongo模块

pymongo的使用

PyMongo下载及安装

使用 pyMongo 创建 ISODate