I am using mongoengine with Django and within my project need to connect to two instances of MongoDB while serving single request. It works just fine if I use:
connect("mdb1")
#do stuff with mdb1
...
connect("mdb2")
#do stuff with mdb2
but I am wondering if that's a proper way of doing it.
I think there is no a proper way to do this. The example of Matt turns obligatory use an connection by Document type. If I want to use one document with many connections the example doesn't fit.
Multiple database support was added in MongoEngine 0.6
Demo using
register_connection
.@Ricardo at the official documentation theres a section explaining context management (i.e switching databases using the same document: http://mongoengine-odm.readthedocs.org/en/latest/guide/connecting.html#context-managers). The following code will switch the class
User
, originally stored inusers-db
to the new databasearchive-user-db