最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

pymongo教程(3)自定义数据类型

来源:懂视网 责编:小采 时间:2020-11-09 13:14:41
文档

pymongo教程(3)自定义数据类型

pymongo教程(3)自定义数据类型:pymongo提供一些常用的数据类型,如:数据、字符串、日期等。如果感觉还不能满足需求,那么还可以自定义数据类型。 首先定义一个类: class Custom(object): def __init__(self, x): self.__x = x def x(self): return self._
推荐度:
导读pymongo教程(3)自定义数据类型:pymongo提供一些常用的数据类型,如:数据、字符串、日期等。如果感觉还不能满足需求,那么还可以自定义数据类型。 首先定义一个类: class Custom(object): def __init__(self, x): self.__x = x def x(self): return self._

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

要将自定义类型的数据存入数据库中需要先进行编码;将数据从数据库读取出来后又需要再解码。

手动编码/解码

我们可以定义两个方法,在插入和查询数据时进行手动的编码、解码。

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())

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

pymongo教程(3)自定义数据类型

pymongo教程(3)自定义数据类型:pymongo提供一些常用的数据类型,如:数据、字符串、日期等。如果感觉还不能满足需求,那么还可以自定义数据类型。 首先定义一个类: class Custom(object): def __init__(self, x): self.__x = x def x(self): return self._
推荐度:
标签: 数据 自定义 教程
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top