mongoengine上的多连接..给我一些例子~
我将 mongoengine 与 django 一起使用。
在我的项目Web应用程序中,我需要连接至少两台服务器:一台用于会话本地服务器,另一台连接到mongolab(mongodb托管服务公司)。
所以..我需要连接本地主机进行会话..同时连接到远处的另一台服务器。
如何在 mongoengine 上实现多个连接?
请给我一些例子。
a Web application --- connecting ---> localhost for session
--- connecting ---> mongolab for application database
I'm using mongoengine with django.
in my project web application, I need to connect at least two servers: one that is local for session, another connecting to mongolab (mongodb hosting service company).
So.. I need to connect localhost for session.. while connecting to another server in distance.
How can I implement multiple connections on mongoengine?
give me some examples please.
a Web application --- connecting ---> localhost for session
--- connecting ---> mongolab for application database
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 mongoengine 开发分支。
支持带有别名的多个数据库连接。 https://github.com/hmarr/mongoengine/commit/8d2bc444bb64265f78f5bf716f773742dddd56c1
查看此测试。
https://github.com/hmarr/mongoengine/blob/dev /tests/document.py#L2584
Use mongoengine dev branch.
Support multiple dbconnection with alias. https://github.com/hmarr/mongoengine/commit/8d2bc444bb64265f78f5bf716f773742dddd56c1
See this tests.
https://github.com/hmarr/mongoengine/blob/dev/tests/document.py#L2584
根据 mongoengine 文档,要使用多个数据库,您可以使用
connect()
并为连接提供一个别名。在后台,它使用register_connection()
来存储数据,如果需要,您可以预先注册所有别名。您还可以使用
switch_db()
方法。函数disconnect()可用于断开特定连接。这可用于全局更改连接:
Depending on mongoengine documentation, to use multiple databases you can use
connect()
and provide an alias name for the connection. In the background this usesregister_connection()
to store the data and you can register all aliases up front if required.You can also use
switch_db()
method.The function disconnect() can be used to disconnect a particular connection. This can be used to change a connection globally: