PyMongo 编码错误:bson.errors.InvalidDocument:无法编码对象

问题描述 投票:0回答:2

我正在尝试将自定义对象插入到 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'>
    
python database mongodb pymongo
2个回答
0
投票
看起来代码量很大;怎么样:

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 } }
    

0
投票
如果您尝试在 mongodb 中插入类对象,则需要将其转换为字典表示形式。您无法插入直接对象。

{"key": class_object__dict__}
    
© www.soinside.com 2019 - 2024. All rights reserved.