首页 > 解决方案 > PyMongo 编码错误:bson.errors.InvalidDocument:无法编码对象

问题描述

我正在尝试将自定义对象插入 PyMongo 数据库集合。它抛出了一个InvalidDocument错误,在 StackOverflow 上搜索并在 PyMongo docs上搜索后,我找到了一个解决方案。但是,它仍然给我一个InvalidDocument错误。这是代码:

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 (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'>

标签: pythondatabasemongodbpymongo

解决方案


看起来像大量的代码;怎么样:

class Custom(object):

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

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

数据库中的结果:

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

推荐阅读