2016-11-06 60 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')