How can I use the mongolab add-on to Heroku from python?

前端 未结 5 1329
旧时难觅i
旧时难觅i 2021-01-14 13:43

The documentation only talks about how to do it from ruby.

5条回答
  •  爱一瞬间的悲伤
    2021-01-14 14:26

    I'm using the following:

    import os
    from urlparse import urlsplit
    from pymongo import Connection
    
    url = os.getenv('MONGOLAB_URI', 'mongodb://localhost:27017/testdb')
    parsed = urlsplit(url)
    db_name = parsed.path[1:]
    
    # Get your DB
    db = Connection(url)[db_name]
    
    # Authenticate
    if '@' in url:
        user, password = parsed.netloc.split('@')[0].split(':')
        db.authenticate(user, password)
    

提交回复
热议问题