python操作mongodb之六自定义类型存储
from pymongo.mongo_client import MongoClient
client=MongoClient('192.168.30.252',27017)
client=drop_database('custom_type_example')
db=client.custom_type_exampleclass Custom(object):def __init__(self,x):self.__x=xdef x(self):return self.__x###上面的类 不能自动编码 需要手动编码 如下
#将custom编码成json格式
def encode_custom(custom):return {"_type": "custom", "x": custom.x()}
#将document还原成custom类
def decode_custom(document):assert document["_type"] == "custom"return Custom(document["x"])
db.test.insert({"custom": encode_custom(Custom(5))})
db.test.find_one()
decode_custom(db.test.find_one()["custom"])
decode_custom(db.test.find_one()["custom"]).x()foo=Custom(10)
foo.x()#手动显得繁琐,使用自动的吧
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-docsson[key] = self.transform_incoming(value, collection)return sondef 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-docsson[key] = self.transform_outgoing(value, collection)return son
#加入操纵者入数据库
db.add_son_manipulator(Transform())
#插入对象类型
db.test.insert({"custom": Custom(5)})
db.test.find_one()
#使用对象类型
db.test.find_one()["custom"].x()
5def to_binary(custom):return Binary(str(custom.x()), 128)def from_binary(binary):return Custom(int(binary))#二进制编码
from bson.binary import Binary
from pymongo.son_manipulator import SONManipulator
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 sondef 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())
转载于:https://www.cnblogs.com/similarface/p/5614039.html
python操作mongodb之六自定义类型存储相关推荐
- mongodb python 大于_5、Python操作MongoDB
Mongodb MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能.MongoDB 旨在为WEB应用提供可扩展的高性能 ...
- MongoDB的安装到使用 + Python操作MongoDB + 有关系和无关系数据库比较
day1 - day7 mongoDB学习第一天 数据库 MongoDB (芒果数据库) 数据存储阶段 文件管理阶段 (.txt .doc .xls) 优点 : 数据可以长期保存 可以存储大量的数据 ...
- MongoDB - 使用Python操作MongoDB
使用Python操作MongoDB数据库 CSDN:jcLee95 邮箱 :291148484@163.com CSDN 主页:https://blog.csdn.net/qq_28550263?sp ...
- python 读取mongodb,python操作MongoDB
python操作mongodb 连接mongodb #!/usr/bin/env python # -*- coding:utf-8 -*- from pymongo import MongoClie ...
- Python 操作 MongoDB
MongoDB 教程.高级教程:https://www.runoob.com/mongodb/mongodb-tutorial.html 官网:https://www.mongodb.com/ Pym ...
- python redis缓存_第二百九十五节,python操作redis缓存-字符串类型
python操作redis缓存-字符串类型 首先要安装redis-py模块 python连接redis方式,有两种连接方式,一种是直接连接,一张是通过连接池连接 注意:以后我们都用的连接池方式连接,直 ...
- Python 操作 MongoDB 数据库!
作者 |黄伟呢 来源 |数据分析与统计学之美 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 先来看看MySQL与MongoDB 概念区别 ...
- python操作mongodb进行读写
python操作mongodb 安装mongodb的Python库 python3 -m pip install pymongo 测试连接: import pymongo #myclient = py ...
- 使用Python操作MongoDB
微信公众号:运维开发故事,作者:double冬 通常在项目中,一般都需要一种编程语言来操作数据库,使用Python来操作数据库有着天然的优势,因为Python的字典和MongoDB的文档几乎是一样的格 ...
最新文章
- iOS更改AppIcon
- python去除rpm仓库中同名低版本的包
- P1156-垃圾陷阱【dp】
- SharePoint网站集备份与恢复
- Jquery Datatable的使用样例(ssm+bootstrsp框架下)服务器端分页
- 数据库索引如何工作?原理
- [.NET 4.5] ADO.NET / ASP.NET 使用 Async 和 Await 异步 存取数据库
- SQL Server内存
- 关于Axure原型在Axure share上的尺寸问题。
- 线性回归介绍之十——多因素分析策略
- 经典论文之OverFeat
- 医学成像中的深度学习——基于PyTorch的3D 医学图像分割
- Word文档的使用技巧
- python课程设计小结和体会_通用版课程设计心得体会
- Android开发这么久你竟然还不知道Dropbox?
- 用jq做一个点击图片放大消失
- 计算机硬件清单3000元,3000元锐龙apu电脑主机推荐ryzen5 2400G电脑配置清单
- Lua学习笔记(2023-2)
- servlet cannot be resolved to a type的原因及解决方法
- 批量清理QQ好友的方法