pythonmongodbpymongotornado-motordatabase

How to connect to a mongoDB


I'm using mongoLabs to host my database and I want to connect to it from my app.

I'm also using the Motor module in pyMongo. I'm unsure where to instantiate the connection.

For instance I know that if the database was on same local machine as the app I would do this:

 database = motor.MotorClient().open_sync().myDatabase

The mongoLab site says to include the following uri in the driver:

mongodb://<dbuser>:<dbpassword>@ds047057.mongolab.com:47057/myDatabase

But I cannot see how to create the connection to this database.

Thanks


Solution

  • It looks like MotorClient takes the same arguments as MongoClient:

    https://github.com/ajdavis/mongo-python-driver/blob/motor/motor/init.py#L782

    http://api.mongodb.org/python/current/api/pymongo/mongo_client.html

    Given that, you should be able to use the URI like so:

    database = motor.MotorClient("mongodb://<dbuser>:<dbpassword>@ds047057.mongolab.com:47057/myDatabase").open_sync().myDatabase