bson.errors.InvalidDocument:无法对具有对象列表的对象的对象进行编码

发布于 2025-01-20 21:10:39 字数 2788 浏览 1 评论 0原文

我有一个类别在下面的类别中定义的类

class B():
  xyz: int

class C():
  abc: int

Class A():
 bList: List[B]
 cList: List[C]
 iex : int

MongoDB保存操作:

client = pymongo.MongoClient("xxx")
mydb = client["XYZ"]
mycol = mydb["DB"]
mydict=A.__dict__
 x=mycol.insert_one(mydict) 

如果我尝试保存对象而没有A中的B和C列表,而将IEX保存为INT,则它将获得保存。

使用其他对象的列表,即使我创建了该对象

日志的dict,它也会引发异常:

文件“ c:\ python \ python310 \ lib \ site-packages \ pymongo \ collection.py”, 第474行,在_insert_one中 self .__ database.client._retryable_write(文件“ c:\ python \ python310 \ lib \ lib \ site-packages \ pymongo \ pymongo \ mongo_client.py,line”,line 1340,在_retryable_write
中 返回self。 1229,在_retry_with_session中 返回self。 1261,在_retry_internal
中 返回func(session,sock_info,可重试)文件“ c:\ python \ python310 \ lib \ lib \ site-packages \ pymongo \ collection.py”,line 462,在_insert_command
中 结果= sock_info.command(file“ c:\ python \ python \ python310 \ lib \ lib \ site-packages \ pymongo \ poom.py”,第735行,in 命令 self._raise_connection_failure(error)文件“ c:\ python \ python310 \ lib \ site-packages \ pymongo \ poom.py.py”,第719行,in 命令 返回命令(self,dbname,spec,secondary_ok,文件“ c:\ python \ python310 \ lib \ lib \ site-packages \ pymongo \ pymongo \ network.py”,第118行, 在命令中 request_id,msg,size,max_doc_size = message._op_msg(文件“ c:\ python \ python \ python310 \ lib \ lib \ lib \ site-packages \ pymongo \ pymongo \ message.py”,第597行,第597页,第597页 在_op_msg中 返回_op_msg_uncompressed( bson.errors.invaliddocument:code ocdode object:< bb对象at 0x000001dd37c85570> of类型: 'bb'>

任何解决此问题的指针。

  • 我尝试创建一个列表,然后插入_any,同样的问题。

  • 试图保存对象而不转换为同一问题。

  • getDump也会给出同样的问题。

任何指针或链接都会有所帮助。尝试使用许多链接,但它们没有其他对象列表


更新的对象: 为{'atm':17550,'data'创建的dict对象

:[{...},{...},{...},{...},{...},{...},{.. 。},{...},{...},{...},...],'Price':17561.3,'spot':17530.3,'Total':1091633.0,'total1':28878936, 'total2':623204.0,'total3':23602622,'total4':437333.0,'total5':623204.0:623204.0,'bdata':[< bb at 0x000001640f695090> 0x000001640f695210>,< bb对象在0x000001640F6952D0> bb对象0x000001640f695390> bb对象>,< bb对象在0x000001640F6955D0>,bb对象at 0x000001640f695690> gt; ,...],“ cdata”:[< cc对象在0x000001640f6950f0>,< cc对象在0x000001640f6951b0 ; CC对象在0x000001640F6953F0> < cc对象在0x000001640f6954b0 17561.3_17530.3_28878936_23602622'}

看起来B和C是字段未正确注入,

更新a类A:

from typing import List, Dict
import datetime

from B import B
from C import C

class A():
    x : str
    y : int
    data: List[Dict[str, float]]
    z: float
    B: list
    C:list
    
    
    def __init__(self):
        pass

I have a class defined in below form

class B():
  xyz: int

class C():
  abc: int

Class A():
 bList: List[B]
 cList: List[C]
 iex : int

MongoDb Save Operation:

client = pymongo.MongoClient("xxx")
mydb = client["XYZ"]
mycol = mydb["DB"]
mydict=A.__dict__
 x=mycol.insert_one(mydict) 

If I tried to save the object without list of B and C in A and keeping on iex as int then it is getting save.

With list of other object, it is throwing exception even if I created dict of that object

logs:

File "C:\Python\Python310\lib\site-packages\pymongo\collection.py",
line 474, in _insert_one
self.__database.client._retryable_write( File "C:\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line
1340, in _retryable_write
return self._retry_with_session(retryable, func, s, None) File "C:\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line
1229, in _retry_with_session
return self._retry_internal(retryable, func, session, bulk) File "C:\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line
1261, in _retry_internal
return func(session, sock_info, retryable) File "C:\Python\Python310\lib\site-packages\pymongo\collection.py", line
462, in _insert_command
result = sock_info.command( File "C:\Python\Python310\lib\site-packages\pymongo\pool.py", line 735, in
command
self._raise_connection_failure(error) File "C:\Python\Python310\lib\site-packages\pymongo\pool.py", line 719, in
command
return command(self, dbname, spec, secondary_ok, File "C:\Python\Python310\lib\site-packages\pymongo\network.py", line 118,
in command
request_id, msg, size, max_doc_size = message._op_msg( File "C:\Python\Python310\lib\site-packages\pymongo\message.py", line 597,
in _op_msg
return _op_msg_uncompressed(bson.errors.InvalidDocument: cannot encode object: <B.B object at 0x000001DD37C85570>, of type: <class
'B.B'>

Any pointer to fix this issue.

  • I have tried to create a list and then insert_many , same issue.

  • Tried to save the object without converting into dict, same issue.

  • getDump also will give the same issue.

Any pointer or link will be helpful. Tried with many link present, but they dont have the object having list of other object


Update:
The dict object created for

{'atm': 17550, 'data': [{...}, {...}, {...}, {...}, {...}, {...}, {...}, {...}, {...}, ...], 'price': 17561.3, 'spot': 17530.3, 'total': 1091633.0, 'total1': 28878936, 'total2': 623204.0, 'total3': 23602622, 'total4': 437333.0, 'total5': 623204.0, 'BData': [<B.B object at 0x000001640F695090>, <B.B object at 0x000001640F695150>, <B.B object at 0x000001640F695210>, <B.B object at 0x000001640F6952D0>, <B.B object at 0x000001640F695390>, <B.B object at 0x000001640F695450>, <B.B object at 0x000001640F695510>, <B.B object at 0x000001640F6955D0>, <B.B object at 0x000001640F695690>, ...], 'CData': [<C.C object at 0x000001640F6950F0>, <C.C object at 0x000001640F6951B0>, <C.C object at 0x000001640F695270>, <C.C object at 0x000001640F695330>, <C.C object at 0x000001640F6953F0>, <C.C object at 0x000001640F6954B0>, <C.C object at 0x000001640F695570>, <C.C object at 0x000001640F695630>, <C.C object at 0x000001640F6956F0>, ...], 'key': '17550_17561.3_17530.3_28878936_23602622'}

Look like B and C are fields are not properly injected

update the Class A:

from typing import List, Dict
import datetime

from B import B
from C import C

class A():
    x : str
    y : int
    data: List[Dict[str, float]]
    z: float
    B: list
    C:list
    
    
    def __init__(self):
        pass

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文