I'm creating a wrapper to solve all my connections with mongoengine, so I created a function that reads mongoDB configuration from a file and connects to it.
Thee function looks like this:
def connect_mongo_odm(config_file_location, db_name):
if db_name:
base_path = ['databases', db_name]
conf_specs = {
'host': {
'path': base_path + ['host']
},
'port': {
'path': base_path + ['port']
}
}
fileConfiguration = dao_utils.readConfiguration(config_file_location, conf_specs)
auth = None
host = fileConfiguration.get('host', None)
host = "mongodb://" + host
connect(alias=db_name,
host=host,
socketKeepAlive=True, socketTimeoutMS=30000)
And I use it as:
# import previous function
# This is another module in my application
connect_mongo_odm('/path/to/config/file', 'dbName')
But When I try to save a document I get an exception saying that I have no default connection defined.