2016-11-06 59 views
1

我不确定如何连接到使用mongoengine使用验证数据库的mongodb数据库。使用验证数据库的mongoengine

在命令提示符下,我需要做mongo hostname:27017/myApp -u "test" -p "test" --authenticationDatabase admin,但我没有看到我将它作为mongoengine的参数传递到哪里,所以我使用admin数据库进行身份验证,但连接到我的模型的myApp数据库?

我相信这是它的PyMongo引导In公司解释说:

https://api.mongodb.com/python/current/examples/authentication.html

>>> from pymongo import MongoClient 
>>> client = MongoClient('example.com') 
>>> db = client.the_database 
>>> db.authenticate('user', 'password', source='source_database') 

我发现添加这mongoengine拉入请求:

https://github.com/MongoEngine/mongoengine/pull/590/files

它看起来像你刚刚添加authentication_source作为connect的参数,如connect(authentication_source='admin')。如果它有更好的文档记录,那会很好。

http://docs.mongoengine.org/apireference.html?highlight=authentication_source

回答

4

根据该mongoengine connecting guide,所述connect()方法支持URI样式连接。即

connect(
    'project1' 
    host='mongodb://username:[email protected]:port1/databaseName' 
) 

在这个意义上,也可以指定如下的认证源数据库:

"mongodb://username:[email protected]:port1/database?authSource=source_database" 

MongoDB connection string URI中查看MongoDB的URI的例子。 也Authentication options through connection string

+0

这不是我要求的,认证源需要明确说明,所以你发送的东西不会工作 – Rob

+1

@Rob更新。我已经从链接中提取信息,让您通过URI指定验证源。 –

1

该解决方案建议不适用于我。什么工作: 只需要为connect方法添加authSource参数,就像使用pymongo MongoClient方法一样。例如:

connect('database_name', host='host', username="username", 
password="password",authSource='authentication_database_name')