python通过mongoengine中connect函数连接多个数据库
mongoengine支持程序同时连接多个数据库,这些数据库可以位于一个或多个mongo之中,通过alias名称区分不同的连接即可。
可以通过switch_db切换到不同的数据库,进行读写操作,switch_db其实是一个上下文管理器,通过和with语句一起使用,能够保证切换数据库的影响范围。
由于个人电脑上没有安装mongodb,以下代码示例中访问的mongodb通过mongoengine提供的mock mongodb替代,只需要在connect函数参数中加上is_mock=True参数,并且安装有mongomock package(pip install mongomock)即可。
#!/usr/bin/env python
# coding=utf-8
from mongoengine import connect
from mongoengine.document import Document
from mongoengine.fields import StringField, IntField
from mongoengine.context_managers import switch_db # 连接不同db
connect(alias='testdbA', is_mock=True)
connect(alias='testdbB', is_mock=True)
connect(alias='testdbC', is_mock=True) class User(Document):
meta = {
# db_alias用于指定User绑定的mongo连接,和connect函数中的alias对应
'db_alias': 'testdbA',
}
uname = StringField(max_length=63)
uid = StringField(max_length=64)
age = IntField(default=0) def create_record(uname, uid, age):
user = User()
user.uname = uname
user.uid = uid
user.age = age
return user def print_records(records):
# 输出User类此时绑定的db连接名称,并输出其记录个数
records = list(User.objects.all())
print('{} count:{}, records:'.format(User._meta['db_alias'], len(records)))
for rc in records:
print(' {}|{}|{}'.format(rc['uname'], rc['uid'], rc['age'])) records = list(User.objects.all())
print_records(records)
create_record('Ace', 'AX001', 10).save()
print_records(records) # switch到testdbB连接
with switch_db(User, 'testdbB'):
print_records(records)
create_record('Bob', 'BX001', 11).save()
print_records(records) # switch到testdbC连接
with switch_db(User, 'testdbC'):
print_records(records)
create_record('Carl', 'CX001', 12).save()
print_records(records)
运行结果如下:
$ python mongo_test.py
testdbA count:0, records:
testdbA count:1, records:
Ace|AX001|10
testdbB count:0, records:
testdbB count:1, records:
Bob|BX001|11
testdbC count:0, records:
testdbC count:1, records:
Carl|CX001|12
switch_db其实就是一个简单的上下文管理器,源码位于mongoengine/context_managers.py当中,其实就是在switch_db对象初始化时、进入with代码块前、离开with代码块后,通过改变db_alias和collection两个字段对应的取值,实现了不同mongodb连接的切换,代码简单注释版本如下:
class switch_db(object):
"""switch_db alias context manager. Example :: # Register connections
register_connection('default', 'mongoenginetest')
register_connection('testdb-1', 'mongoenginetest2') class Group(Document):
name = StringField() Group(name='test').save() # Saves in the default db with switch_db(Group, 'testdb-1') as Group:
Group(name='hello testdb!').save() # Saves in testdb-1
""" def __init__(self, cls, db_alias):
"""Construct the switch_db context manager :param cls: the class to change the registered db
:param db_alias: the name of the specific database to use
"""
self.cls = cls
# 存储切换前的collection
self.collection = cls._get_collection()
# 设置为要切换的db连接名称
self.db_alias = db_alias
# 存储切换前的原db连接名称
self.ori_db_alias = cls._meta.get('db_alias', DEFAULT_CONNECTION_NAME)
def __enter__(self):
"""Change the db_alias and clear the cached collection."""
# 进入with代码块前触发
# 修改为本次指定的db连接名称
self.cls._meta['db_alias'] = self.db_alias
# 将collection置为None,于是之后会重新根据新的db连接获取collection
self.cls._collection = None
return self.cls def __exit__(self, t, value, traceback):
"""Reset the db_alias and collection."""
# 退出with代码块后触发
# 恢复为切换前的db连接名称
self.cls._meta['db_alias'] = self.ori_db_alias
# 恢复为切换前的collection
self.cls._collection = self.collection
python通过mongoengine中connect函数连接多个数据库的更多相关文章
- python 在机器学习中应用函数
浅述python中argsort()函数的用法 (1).先定义一个array数据 1 import numpy as np 2 x=np.array([1,4,3,-1,6,9]) (2).现在我们可 ...
- python开发_python中的函数定义
下面是我做的几个用列: #python中的函数定义,使用和传参 def_str = '''\ python中的函数以如下形式声明: def 函数名称([参数1,参数2,参数3......]): 执行语 ...
- python unittest框架中addCleanup函数详解
接上一篇doCleanups说明,这次介绍下另一个很好用的函数:addCleanup 还是老规矩,看官方文档说明: addCleanup(function, *args, **kwargs)¶ Add ...
- 一、MySQL中的索引 二、MySQL中的函数 三、MySQL数据库的备份和恢复 四、数据库设计和优化(重点)
一.MySQL中的索引###<1>索引的概念 索引就是一种数据结构(高效获取数据),在mysql中以文件的方式存在.存储建立了索引列的地址或者指向. 文件 :(以某种数据 结构存放) 存放 ...
- python 利用matplotlib中imshow()函数绘图
matplotlib 是python最著名的2D绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中.通过简单的绘图语 ...
- (转)python类class中_init_函数以及参数self的简单解释
1)_init_函数(方法) #-*- encoding:utf-8 -*- class NewClass(object): def __init__(self,name): print self s ...
- python和numpy中sum()函数的异同
转载:https://blog.csdn.net/amuchena/article/details/89060798和https://www.runoob.com/python/python-func ...
- Python -- 使用模块中的函数
在确定自己不会导入多个同名函数(从不同模块导入)的情况下,你可能不希望在每次调用函数的时候,都要写上模块的名字.那么,可以使用import命令的另外一种形式: >>> from ma ...
- 关于python使用threadpool中的函数单个参数和多个参数用法举例
1.对单个元素的函数使用线程池: # encoding:utf-8 __author__='xijun.gong' import threadpool def func(name): print 'h ...
随机推荐
- Python机器学习神器:sklearn&numpy
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDE0MDMzOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 牛客网多校训练第三场 C - Shuffle Cards(Splay / rope)
链接: https://www.nowcoder.com/acm/contest/141/C 题意: 给出一个n个元素的序列(1,2,...,n)和m个操作(1≤n,m≤1e5),每个操作给出两个数p ...
- UVa 1635 - Irrelevant Elements(二项式系数 + 唯一分解定理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA10820 Send a Table
嘟嘟嘟 [欧拉函数] 大致题意:如果知道f(a, b),就可以求出f(a * k, b * k).现给出一个n,求至少需要知道几个二元组(a, b),使所有的f(x, y)都能求出来.(1 <= ...
- win7 xampp php7 yii2 配置sqlserver
第一步: https://www.microsoft.com/en-us/download/details.aspx?id=20098 下载 如下图 php7 版本 放到 xampp\php ...
- rnnlm学习
rnn-lm: 1.论文 2.公式推导 2.1 http://blog.csdn.net/a635661820/article/details/44462315 3. 工具 lstm-lm 1. 论文 ...
- qt+vs2005新建配置不自动加载Generated Files进工程(个人备份)
工程右键Qt Project Settings 的Moc Directory路径删除 确定,再进入将删除路径加上
- JasperReport4.6生成PDF中文
Web项目中PDF显示中文 本人无奈使用JasperReport4.6,因为这本书(好像也是唯一的一本国内的介绍JasperReport的书), 选择"文件"→New命令,弹出一个 ...
- 一文读懂类加载机制--ClassLoader
一.什么是ClassLoader? 大家都知道,当我们写好一个Java程序之后,不是管是CS还是BS应用,都是由若干个.class文件组织而成的一个完整的Java应用程序,当程序在运行时,即会调用该程 ...
- 使用单例模式来打造ActivityManager类
单例(Singleton)模式 定义 单例模式是一种对象创建型模式,使用单例模式,可以保证为一个类只生成唯一的实例对象.也就是说,在整个程序空间中,该类只存在一个实例对象. GoF对单例模式的定义是: ...