how to switch database name in mongoengine

二次信任 提交于 2019-12-13 03:01:46

问题


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

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.


回答1:


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




回答2:


This isnt currently possible but will be looked at in 0.8. Follow @metoikos's ticket: https://github.com/MongoEngine/mongoengine/issues/106



来源:https://stackoverflow.com/questions/12173469/how-to-switch-database-name-in-mongoengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!