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函数连接多个数据库的更多相关文章

  1. python 在机器学习中应用函数

    浅述python中argsort()函数的用法 (1).先定义一个array数据 1 import numpy as np 2 x=np.array([1,4,3,-1,6,9]) (2).现在我们可 ...

  2. python开发_python中的函数定义

    下面是我做的几个用列: #python中的函数定义,使用和传参 def_str = '''\ python中的函数以如下形式声明: def 函数名称([参数1,参数2,参数3......]): 执行语 ...

  3. python unittest框架中addCleanup函数详解

    接上一篇doCleanups说明,这次介绍下另一个很好用的函数:addCleanup 还是老规矩,看官方文档说明: addCleanup(function, *args, **kwargs)¶ Add ...

  4. 一、MySQL中的索引 二、MySQL中的函数 三、MySQL数据库的备份和恢复 四、数据库设计和优化(重点)

    一.MySQL中的索引###<1>索引的概念 索引就是一种数据结构(高效获取数据),在mysql中以文件的方式存在.存储建立了索引列的地址或者指向. 文件 :(以某种数据 结构存放) 存放 ...

  5. python 利用matplotlib中imshow()函数绘图

    matplotlib 是python最著名的2D绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中.通过简单的绘图语 ...

  6. (转)python类class中_init_函数以及参数self的简单解释

    1)_init_函数(方法) #-*- encoding:utf-8 -*- class NewClass(object): def __init__(self,name): print self s ...

  7. python和numpy中sum()函数的异同

    转载:https://blog.csdn.net/amuchena/article/details/89060798和https://www.runoob.com/python/python-func ...

  8. Python -- 使用模块中的函数

    在确定自己不会导入多个同名函数(从不同模块导入)的情况下,你可能不希望在每次调用函数的时候,都要写上模块的名字.那么,可以使用import命令的另外一种形式: >>> from ma ...

  9. 关于python使用threadpool中的函数单个参数和多个参数用法举例

    1.对单个元素的函数使用线程池: # encoding:utf-8 __author__='xijun.gong' import threadpool def func(name): print 'h ...

随机推荐

  1. C#中的"?"和"??"

    摘自:http://www.cnblogs.com/zxjyuan/archive/2009/10/27/1590795.html 如果你看到C#中的“?”问号脑袋里便充满问号,那么这个贴子便是为你而 ...

  2. ERP系统架构

    分布式.服务化的ERP系统架构设计 ERP之痛 曾几何时,我混迹于电商.珠宝行业4年多,为这两个行业开发过两套大型业务系统(ERP).作为一个ERP系统,系统主要功能模块无非是订单管理.商品管理.生产 ...

  3. hibernate mysql 分页时报错 显示的代码和sql server 类似 select top 1……

    [ERROR][com.alibaba.druid.filter.stat.StatFilter]merge sql error, dbType mysql, sql : select top 1 d ...

  4. 随机以及时间相关函数——C语言描述

    随机相关的函数 头文件 stdlib.h 相关函数 :rand .srand rand( rand C++ Reference ) 函数声明:int rand( void ); rand函数返回一个位 ...

  5. tomcat 8 startup.bat启动乱码问题

    修改Tomcat bin目录下startup.bat文件 将call "%EXECUTABLE%" start %CMD_LINE_ARGS%  改为 call "%EX ...

  6. 第一章 介绍Django

    Django是一个网络开发框架,有了这个框架,能是网站开发更高效有趣,能轻松的创建和维护高质量的网站应用. 这一本书的目的是使读者成为一个Django的专家.主要着重两点.第一,深入的解释Django ...

  7. JAVA JAVA面试题和项目面试核心要点精华总结(想进大公司必看)

    http://blog.csdn.net/ourpush/article/details/53706524 1.常问数据库查询.修改(SQL查询包含筛选查询.聚合查询和链接查询和优化问题,手写SQL语 ...

  8. 将数组打印到txt文件中

    用print_r 将数组打印到txt文件中.     1.function save_log($content='', $file='app') { $logDir = './logs'; $now ...

  9. SVN工具使用总结

    SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subversion ...

  10. Module、__init__.py 文件解析

    一.什么是Module? 一个python Module就是一个模块,本质就是一个.py文件,其中包含了python对象的定义和python语句. 在模块内部,模块名存储在全局变量__name__中, ...