pymongo提供一些常用的数据类型,如:数据、字符串、日期等。如果感觉还不能满足需求,那么还可以自定义数据类型。 首先定义一个类: class Custom(object): def __init__(self, x): self.__x = x def x(self): return self.__x 要将自定义类型的数据存入数
pymongo提供一些常用的数据类型,如:数据、字符串、日期等。如果感觉还不能满足需求,那么还可以自定义数据类型。
首先定义一个类:
class Custom(object):
def __init__(self, x):
self.__x = x
def x(self):
return self.__x
要将自定义类型的数据存入数据库中需要先进行编码;将数据从数据库读取出来后又需要再解码。
我们可以定义两个方法,在插入和查询数据时进行手动的编码、解码。
响应式博客资讯类会员投稿网站模板安装即用,自带人人站CMS内核及企业站展示功能,支持响应式,前端banner轮播图文本均已进行可视化配置,伪静态页面生成,支持内容模型、多语言、自定义表单、筛选、多条件搜索等功能。模板特点:1、安装即用,自带人人站CMS内核及企业站展示功能(产品,新闻,案例展示等),并可根据需要增加表单 搜索等功能(自带模板) 2、支持响应式 3、前端banner轮播图文本均已进行
0
def encode_custom(custom):
return {"_type": "custom", "x": custom.x()}
def decode_custom(document):
assert document["_type"] == "custom"
return Custom(document["x"])
print(db.test.insert({"custom": encode_custom(Custom(5))}))
print(db.test.find_one()['custom'])
手动地进行编码虽然可行,但是还是不太方便。我们还可以使用 SONManipulator 进行自动编码。
from pymongo.son_manipulator import SONManipulator
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())
print(db.test.insert({"custom": Custom(5)}))
print(db.test.find_one())
我们也可以将其编码成二进制进行存储。
from bson.binary import Binary
def to_binary(custom):
return Binary(str(custom.x()), 128)
def from_binary(binary):
return Custom(int(binary))
class TransformToBinary(SONManipulator):
def transform_incoming(self, son, collection):
for (key, value) in son.items():
if isinstance(value, Custom):
son[key] = to_binary(value)
elif isinstance(value, dict):
son[key] = self.transform_incoming(value, collection)
return son
def transform_outgoing(self, son, collection):
for (key, value) in son.items():
if isinstance(value, Binary) and value.subtype == 128:
son[key] = from_binary(value)
elif isinstance(value, dict):
son[key] = self.transform_outgoing(value, collection)
return son
db.add_son_manipulator(TransformToBinary())
print(db.test.insert({"custom": Custom(5)}))
print(db.test.find_one())
原文地址:pymongo教程(3)——自定义数据类型, 感谢原作者分享。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号