0

I am new on MongoAlchemy. Currently i have pymongo which i only need use 1 URL to connect to mongolabs server

mongodb://myusername:secretpassword@ds045801-a1.mongolab.com:45801,ds045808-a0.mongolab.com:45808/mydatabase_name

While MongoAlchemy use MONGOALCHEMY_SERVER but it seems only take 1 host. Is there anyway to use the whole url as connection string? e.g

app.config['MONGOALCHEMY_SERVER'] =  'mongodb://myusername:secretpassword@ds045801-a1.mongolab.com:45801,ds045808-a0.mongolab.com:45808/mydatabase_name'

Cheers

Granit
  • 188
  • 1
  • 11
  • Is this to connect to a replica set? – Joe Doherty Mar 14 '15 at 10:13
  • Yeah i suppose it is a replica since mongolabs provides master and slave nodes. – Granit Mar 15 '15 at 11:35
  • It should work out what is in the replica set for you – Joe Doherty Mar 15 '15 at 11:37
  • No it doesn't work. If you read the source code it takes only 1 server. Return will then merge the host,port as string... – Granit Mar 15 '15 at 13:03
  • It should be a PyMongo thing http://api.mongodb.org/python/current/examples/high_availability.html#id1 – Joe Doherty Mar 15 '15 at 13:10
  • We use pymongo in previous projects but this time is Flask-MongoAlchemy, which you cannot add multiply hosts in the app.config['MONGOALCHEMY_SERVER']. It uses _get_mongo_uri and `return with uri = 'mongodb://%s%s:%s/%s%s' % (auth, app.config.get(key('SERVER')), app.config.get(key('PORT')), database, options)` – Granit Mar 16 '15 at 11:33
  • Sorry I miss read I thought you were on about Pymongo. Look here https://github.com/cobrateam/flask-mongoalchemy/blob/master/flask_mongoalchemy/__init__.py#L101. There is a REPLICA_SET option but that is not what you are after. I guess send them a pull request. – Joe Doherty Mar 16 '15 at 11:37

1 Answers1

0

Update: If anyone have same issue, version 0.7.1 is now support with connection string: https://github.com/cobrateam/flask-mongoalchemy/issues/40


I think i found a away to pass 2 hosts. Since the _get_mongo_uri expects

mongodb://%s%s:%s/%s%s' % (auth, app.config.get(key('SERVER')), app.config.get(key('PORT')), database, options)

I just insert the

mongodb://myusername:secretpassword@ds045801-a1.mongolab.com:45801,ds045808-a0.mongolab.com:45808/mydatabase_name

And the result is: app.config['MONGOALCHEMY_SERVER'] = "ds045801-a1.mongolab.com:45801,ds045808-a0.mongolab.com"

It's a bit ugly but it works (for now)

The mongodb connection string format is like this

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

http://docs.mongodb.org/manual/reference/connection-string/

Granit
  • 188
  • 1
  • 11