Skip to content

Instantly share code, notes, and snippets.

@kennethreitz
Forked from lstoll/mongo.py
Created March 13, 2012 22:54
Show Gist options
  • Select an option

  • Save kennethreitz/2032410 to your computer and use it in GitHub Desktop.

Select an option

Save kennethreitz/2032410 to your computer and use it in GitHub Desktop.
MongoHQ w/ pymongo on Heroku
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]
@RyanMcG
Copy link
Copy Markdown

RyanMcG commented Jun 3, 2012

This was very helpful! Thanks!

@aaronbot
Copy link
Copy Markdown

aaronbot commented Aug 6, 2012

Thanks this helped!

@ddohnggo
Copy link
Copy Markdown

ditto to everyone above. this is great. however, i needed to include "from urlparse import urlparse" in my code.

@xru
Copy link
Copy Markdown

xru commented Feb 17, 2014

This is the "python-mongo-heroku" answer, thanks.

@artburkart
Copy link
Copy Markdown

I wish MongoHQ had this in their documentation... thanks!

@cbefus
Copy link
Copy Markdown

cbefus commented Jul 29, 2014

Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment