Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

RyanMcG commented Jun 3, 2012

This was very helpful! Thanks!

@aaronbot

This comment has been minimized.

Copy link

aaronbot commented Aug 6, 2012

Thanks this helped!

@ddohnggo

This comment has been minimized.

Copy link

ddohnggo commented Apr 29, 2013

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

@xru

This comment has been minimized.

Copy link

xru commented Feb 17, 2014

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

@artburkart

This comment has been minimized.

Copy link

artburkart commented Jun 30, 2014

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

@cbefus

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.