from pymongo.mongo_client import MongoClient
client=MongoClient('192.168.30.252',27017)
client=drop_database('custom_type_example')
db=client.custom_type_example class Custom(object):
def __init__(self,x):
self.__x=x
def 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-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())
#插入对象类型
db.test.insert({"custom": Custom(5)})
db.test.find_one()
#使用对象类型
db.test.find_one()["custom"].x()
5 def 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 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())

  

python操作mongodb之六自定义类型存储的更多相关文章

  1. python操作Redis安装、支持存储类型、普通连接、连接池

    一.python操作redis安装和支持存储类型 安装redis模块 pip3 install redis 二.Python操作Redis之普通连接 redis-py提供两个类Redis和Strict ...

  2. 使用Python操作MongoDB

    MongoDB简介(摘自:http://www.runoob.com/mongodb/mongodb-intro.html) MongoDB 由C++语言编写,是一个基于分布式文件存储的开源数据库系统 ...

  3. 第二百九十七节,python操作redis缓存-List类型,可以理解为列表

    python操作redis缓存-List类型,可以理解为列表,是可以有重复元素的列表 List操作,redis中的List在在内存中按照一个name对应一个List来存储.如图: lpush(name ...

  4. 【转】Python操作MongoDB

    Python 操作 MongoDB   请给作者点赞--> 原文链接 这篇文章主要介绍了使用Python脚本操作MongoDB的教程,MongoDB作为非关系型数据库得到了很大的宣传力度,而市面 ...

  5. Python操作MongoDB代码示例

    import pymongo #pip install pymongo安装python操作mongodb的模块 myclient=pymongo.MongoClient(host='127.0.0.1 ...

  6. Python 操作 mongodb 数据库

    原文地址:https://serholiu.com/python-mongodb 这几天在学习Python Web开发,于 是做准备做一个博客来练练手,当然,只是练手的,博客界有WordPress这样 ...

  7. python操作三大主流数据库(10)python操作mongodb数据库④mongodb新闻项目实战

    python操作mongodb数据库④mongodb新闻项目实战 参考文档:http://flask-mongoengine.readthedocs.io/en/latest/ 目录: [root@n ...

  8. MongoDB的安装与python操作MongoDB

    一.安装MongoDB 因为我个人使用的是windows,就只记录下windows下的安装 1.下载安装 就是官网,下载msi,选个路径安装 2.配置 看见别的地方说需要手动在bin同级目录创建dat ...

  9. 第二百九十五节,python操作redis缓存-字符串类型

    python操作redis缓存-字符串类型 首先要安装redis-py模块 python连接redis方式,有两种连接方式,一种是直接连接,一张是通过连接池连接 注意:以后我们都用的连接池方式连接,直 ...

随机推荐

  1. HDU(3605),二分图多重匹配

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    ...

  2. Linux sync命令的作用

    adb shell sync 写缓存命令——sync 在用reboot命令启动unix系统后,系统提示出错信息,部分应用程序不能正常工作.经仔细检查系统文件,并和初始的正确备份进行比较,发现某些文件确 ...

  3. Eclipse启动提示Failed to load the JNI shared library JVM.dll

    一.出现了上述问题解决办法 1.查看eclipse.ini文件 看看eclipse环境架构需要的是什么. plugins/org.eclipse.equinox.launcher.win32.win3 ...

  4. SqlSever基础 getdate函数 返回系统当前的年月日,时分秒 毫秒

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  5. 如何提高android串口kernel log等级

    在 /device/qcom/common/rootdir/etc/init.qcom.rc write /proc/sys/kernel/printk  "6 6 1 7" 第一 ...

  6. 计算5的阶乘并在JSP页面输出

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  7. KEIL MDK 5.12帮你快速建工程模板的技巧

    KEIL 5帮你快速建工程模板的技巧 本人使用keil mdk 5.12有一段时间了,发现keil mdk 5.12里面驱动库比较方便.这个新功能可以节省我们的时间,也可以让初学者能尽快上手和掌握这个 ...

  8. PigSPS: a database for pig SNPs and signatures of positive selection

    URL: http://www.ibiomedical.net/pigsps/ keywords: pig, boar, SNP, positive selection, database, db, ...

  9. CVE-2015-7547

    危险漏洞补丁修复通知 漏洞编号 漏洞编号为CVE-2015-7547 漏洞说明: Google安全团队近日发现glibc存在的溢出漏洞. glibc的DNS客户端解析器中存在基于栈的缓冲区溢出漏洞.当 ...

  10. Python基础学习笔记(十)日期Calendar和时间Timer

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-date-time.html 3. http://www.liao ...