- 
      
 - 
        
Save kennethreitz/2032410 to your computer and use it in GitHub Desktop.  
    MongoHQ w/ pymongo on Heroku
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import os | |
| import pymongo | |
| MONGO_URL = os.environ.get('MONGOHQ_URL') | |
| if MONGO_URL: | |
| # Get a connection | |
| conn = pymongo.Connection(MONGO_URL) | |
| # Get the database | |
| db = conn[urlparse(MONGO_URL).path[1:]] | |
| else: | |
| # Not on an app with the MongoHQ add-on, do some localhost action | |
| conn = pymongo.Connection('localhost', 27017) | |
| db = conn['someapps-db'] | |
| # Test stuff | |
| db.test_collection.insert({"testdoc":"totaltest"}) | |
| print db.test_collection.find()[0] | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | pymongo | 
This is the "python-mongo-heroku" answer, thanks.
I wish MongoHQ had this in their documentation... thanks!
Thanks for this!
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
ditto to everyone above. this is great. however, i needed to include "from urlparse import urlparse" in my code.