Skip to content

Instantly share code, notes, and snippets.

@lstoll
Created March 13, 2012 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lstoll/2026691 to your computer and use it in GitHub Desktop.
Save lstoll/2026691 to your computer and use it in GitHub Desktop.
MongoHQ w/ pymongo on Heroku
import os
import pymongo
if os.environ['MONGOHQ_URL']:
# Get a connection
conn = pymongo.Connection(os.environ['MONGOHQ_URL'])
# Get the database
db = conn[urlparse(os.environ['MONGOHQ_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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment