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 ...
随机推荐
- asp.net mvc 4.0常见的几个问题
看书的时候遇到很多不知所云的错误,都是在网上找到的解决方法,没办法,从asp.net到mcv很多的新技术,没有一点思路,只能在网上搜罗了. 1.更新产品不成功 更新产品的时候一直不能更新成功,但是很奇 ...
- 如何给SAP C4C的产品主数据division配置出新的下拉选项
如图:C4C产品主数据division字段默认的下拉菜单选项: 切换成调试模式,找到UI这个字段绑定的模型字段名称:/Root/MaterialDivision: 再找到这个UI模型字段绑定到的cor ...
- TypeScript----类
一.类的属性 public: 公有, private: 私有,不能在声明它的类的外部访问,只能在类内部访问 protect: 保护,不能在声明它的类的外部访问,但继承者除外 readonly 只读属性 ...
- Ubuntu中为Eclipse添加桌面启动快捷方式
Ubuntu中应用程序启动器“XXX.desktop”还没有被标记为可信任的问题:http://www.tuicool.com/articles/fIBJ32n eclipse问题:prefences ...
- mxnet导入图像数据
图像的标签在一个json文件中. %matplotlib inline import json import gluonbook as gb import mxnet as mx from mxnet ...
- 跟着大神学Mongo,Mongodb主从复制本机简单操作总结
原文链接:http://www.cnblogs.com/huangxincheng/archive/2012/03/04/2379755.html 本机安装MongoDB不在介绍,本文Mongo小菜鸟 ...
- 用HTML编写迪士尼乐园页面
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/html"><head lang="e ...
- CSS技巧之向下箭头
思路:使用◇符号(可在输入法的软键盘找到该符号),使用定位选择位置,并隐藏溢出的上半部分细点:1.使用i标签的楷体属性把◇变大2.给i只有◇符号一半的高度,并隐藏溢出,正常显示一个向上箭头3.对s使用 ...
- 史上最简单的SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f12-dash/ 本文出自方志朋的博客 在我的第四篇文章断路 ...
- 使用nuget过程中一些问题总结
更新System.Web.Http组件以及其相关依赖项使用以下命令更新: Update-Package Microsoft.AspNet.WebApi –reinstall 如果没有这个引用,则先添加 ...