I am trying to insert a custom object into a PyMongo database collection. It threw an InvalidDocument
error, and after searching on StackOverflow, and searching on PyMongo docs, I found a solution. However, it is still giving me an InvalidDocument
error. Here's the code:
import pymongo
from pymongo.son_manipulator import SONManipulator
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
db = myclient["testdb"]
col = db["testcol"]
class Custom(object):
def __init__(self, x):
self.__x = x
def x(self):
return self.__x
def encode_custom(custom):
return {"_type": "custom", "x": custom.x()}
def decode_custom(document):
assert document["_type"] == "custom"
return Custom(document["x"])
class Transform(SONManipulator):
def transform_incoming(self, son, collection):
for (key, value) in son.items():
if isinstance(value, Custom):
son[key] = encode_custom(value)
elif isinstance(value, dict): # Make sure we recurse into sub-docs
son[key] = self.transform_incoming(value, collection)
return son
def transform_outgoing(self, son, collection):
for (key, value) in son.items():
if isinstance(value, dict):
if "_type" in value and value["_type"] == "custom":
son[key] = decode_custom(value)
else: # Again, make sure to recurse into sub-docs
son[key] = self.transform_outgoing(value, collection)
return son
db.add_son_manipulator(Transform())
col.insert_one({"custom": Custom(5)}) #Line that caused error
Traceback:
Traceback (most recent call last):
File "/Users/jay/Projects/RPG/test.py", line 47, in <module>
col.insert_one({"custom": Custom(5)})
File "/usr/local/lib/python3.9/site-packages/pymongo/collection.py", line 698, in insert_one
self._insert(document,
File "/usr/local/lib/python3.9/site-packages/pymongo/collection.py", line 613, in _insert
return self._insert_one(
File "/usr/local/lib/python3.9/site-packages/pymongo/collection.py", line 602, in _insert_one
self.__database.client._retryable_write(
File "/usr/local/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1498, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "/usr/local/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1384, in _retry_with_session
return self._retry_internal(retryable, func, session, bulk)
File "/usr/local/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1416, in _retry_internal
return func(session, sock_info, retryable)
File "/usr/local/lib/python3.9/site-packages/pymongo/collection.py", line 590, in _insert_command
result = sock_info.command(
File "/usr/local/lib/python3.9/site-packages/pymongo/pool.py", line 699, in command
self._raise_connection_failure(error)
File "/usr/local/lib/python3.9/site-packages/pymongo/pool.py", line 683, in command
return command(self, dbname, spec, slave_ok,
File "/usr/local/lib/python3.9/site-packages/pymongo/network.py", line 120, in command
request_id, msg, size, max_doc_size = message._op_msg(
File "/usr/local/lib/python3.9/site-packages/pymongo/message.py", line 714, in _op_msg
return _op_msg_uncompressed(
bson.errors.InvalidDocument: cannot encode object: <__main__.Custom object at 0x10a67abe0>, of type: <class '__main__.Custom'>
question from:
https://stackoverflow.com/questions/65914156/pymongo-encoding-error-bson-errors-invaliddocument-cannot-encode-object