'bson.errors.InvalidDocument: cannot encode object of object having list of Object

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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source