python - 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'>
解决方案
看起来像大量的代码;怎么样:
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 } }
推荐阅读
- reactjs - Cypress 组件测试、ReactJS 和 TailwindCSS
- ruby-on-rails - Linux mint mate - ruby 3.0.2 - zlib.so 未定义符号:inflateReset
- git - 无法将新更新推送到远程
- api - 如何使用 CoinMarketCap api 仅请求一种加密货币的数据?
- reactjs - 试图通过渲染器在 reactjs 中发出警报
- android - react-native-video :使用反应导航导航到另一个屏幕时视频停止但在 iOS 上不停止
- html - 如何创建垂直下拉菜单动画菜单
- reactjs - 带有浏览器后退/前进按钮的浅层路由 NEXT.js
- python-3.x - Flask API 和 mongoDB 在 Ubuntu VM 上使用 docker-compose 运行,但是当我在带有 docker 桌面的 Windows 上运行它们时,没有响应
- bash - 重命名不会重命名文件,尽管可以清楚地检测到它们