The documentation only talks about how to do it from ruby.
I think something like this should work:
import os
import sys
import pymongo
mongo_url = os.getenv('MONGOLAB_URI', 'mongodb://localhost:27017')
db_name = 'mongotest'
if __name__ == '__main__':
try:
connection = pymongo.Connection(mongo_url)
if 'localhost' in self.mongo_url:
db_name = 'my_local_db_name'
else:
db_name = self.mongo_url.rsplit('/',1)[1]
database = connection[db_name]
except:
print('Error: Unable to Connect')
connection = None
if connection is not None:
database.test.insert({'name': 'foo'})