Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

python - PyMongo Encoding Error: bson.errors.InvalidDocument: cannot encode object

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Seems like a huge amount of code; how about:

class Custom(object):

    def __init__(self, x):
        self.x = x

col.insert_one({"custom": Custom(5).__dict__})

Result in database:

> db.testcol.find({}, {'_id': 0})
{ "custom" : { "x" : 5 } }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...