烧瓶MongoEngine - 如何更改数据库? [英] Flask MongoEngine - How to change database?
问题描述
我有一个Flags应用程序,其中包含一个 Post
模型,它跨越多个MongoDB数据库(db1,db2,db3,...),所以我需要能够查询不同的dbs。
我使用 Flask-MongoEngine
扩展名。
$ b
我的 __ init __。py
包含以下行:
db = MongoEngine(app)
和 config.py
我有:
MONGODB_SETTINGS = {'DB':'db1'}
$ ol
db
对象中的连接
参数像这样: db.connection = mongoengine.connect('db2')
它没有改变任何东西。执行
post = Post.objects.all()
仍然运行在原始数据库( db1
)上。 Post class meta
中创建别名,如下所示:
'db_alias':'db1'
然后我改变了连接
参数在这样的 db
对象中:
db.connection = mongoengine.connect('db2',alias ='db1' )
这确实会创建一个连接到 db2
,但是我无法更改它到另一个分贝。
MONGODB_SETTINGS
设置为 {'DB':'db2'}
和那么 db = MongoEngine(app)
这也行不通
好像一旦 models.py
中的模型已经被加载(或者连接到一个db),你就不能改变它的连接。
是否正确?
如何在同一个模型上更改数据库?
编辑:Django等价于使用$ c ORM中的$ c>参数,它允许查询不同的数据库的相同模型。
> MongoEngine 实例,因此从它创建的所有文档(模型)都被绑定到一个特定的数据库。
您可以(暂时)通过调用实例中的 switch_db
方法,将一个文档实例添加到文档类中定义的别名中:
Post.obje保存()
将保存所有文档数据库 或者有很多方法可以使初始配置成为动态,例如尊重环境变量: 然而,从您的评论 我们的系统使用多个数据库来托管这个模型 看起来你可能需要的是 Sharding ,MongoDB负责分发集合多个机器上有多个 I have Flask app with a My I tried the following without success: It seems like once the model in As you correctly identified, a MongoEngine instance and consequently all the documents (models) created from it are bound to a particular database. You can (temporarily) change the database used by a document instance to an alias defined in the document's class by calling the would save all documents in the database Alternatively there are a number of ways to make the initial configuration "dynamic" and e.g. respect environment variables: However, from your comment Our system uses a number of databases to host this model it seems that what you probably want is Sharding, where MongoDB takes care of distributing a collection over multiple 这篇关于烧瓶MongoEngine - 如何更改数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! db1
if db1
定义为 db_alias
Post 类(否则你会得到一个
ConnectionError
)。 b
$ b
pre $ import $ $ b $ app ['MONGODB_SETTINGS'] = {'db':os.environ。 get('DBNAME','db2')}
mongod
个实例。在这种情况下,你可以连接到一个 mongos
,它负责将查询路由到右边的分片。数据库名称在每个分片上都是相同的。Post
model which spans across multiple MongoDB databases (db1, db2, db3,...), so I need to be able to query different dbs.
I'm using Flask-MongoEngine
extension. __init__.py
contains the following line:
db = MongoEngine(app)
and in config.py
I have:
MONGODB_SETTINGS = {'DB': 'db1'}
connection
parameter in the db
object like this:
db.connection = mongoengine.connect('db2')
It didn't change anything. Executing post = Post.objects.all()
still ran on the original db (db1
). Post
class meta
like this:
'db_alias': 'db1'
Then I altered the connection
parameter in the db
object like this:
db.connection = mongoengine.connect('db2', alias='db1')
This would indeed create a connection to db2
, but I couldn't change it to another db. MONGODB_SETTINGS
to {'DB': 'db2'}
and then db = MongoEngine(app)
This doesn't work as well models.py
has been loaded (or connected to a db), you cannot change its connection.
Is that correct?
How can I change databases on the same model?
EDIT: The Django equivalent is the using
parameter in the ORM which allows querying different DBs for the same model.switch_db
method on the instance:Post.objects.all().switch('db1').save()
db1
if db1
was defined as db_alias
in the Post
class (otherwise you'll get a ConnectionError
).import os
app['MONGODB_SETTINGS'] = {'db': os.environ.get('DBNAME', 'db2')}
mongod
instances on multiple machines. In that case you connect to a mongos
instead, which takes care of routing a query to the right shard. The database name is the same on each shard.