如何在mongoengine中切换数据库名称 [英] how to switch database name in mongoengine

查看:213
本文介绍了如何在mongoengine中切换数据库名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个具有相同集合名称,文档类型等的数据库.

I have multiple databases with same collection names, document types, etc.

在Github的源代码中,文档使用get_db方法选择要处理的数据库,在该方法中,还有另一个调用来获取服务器连接.这两个过程都使用称为别名"的相同参数进行工作.

In source code at Github, documents use get_db method to choose the database to work on and in that method there is another call to get server connection. Both of these processes works with same parameter that called "alias".

比方说,我用相同的服务器详细信息和不同的数据库,别名定义了所有数据库连接.这样,将有多个连接到同一服务器.但是如果我可以在单个连接上更改数据库,那就更好了.例如;

Let's say that I have all my db connections defined with same server details and different databases, aliases. In that way, there will be multiple connections to same server. But it would be better if I could change database on a single connection. For example;

    import pymongo
    con = pymongo.connection(/* ... */)
    def dummyDBSwitch(db):
            global con
            return con[db]

是否可以在查询之前切换数据库? 谢谢.

Is it possible to switch database before querying? Thanks.

推荐答案

我们向mongoengine源代码添加了一些方法,并通过这种方式解决了此需求,详细信息:

We add some methods to mongoengine source and fix this need this way, details: https://github.com/cool-shark/mongoengine

这篇关于如何在mongoengine中切换数据库名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆