Skip to content

Instantly share code, notes, and snippets.

@songxiaofeng1981
Forked from kennethreitz/mongo.py
Created October 25, 2012 06:08
Show Gist options
  • Save songxiaofeng1981/3950791 to your computer and use it in GitHub Desktop.
Save songxiaofeng1981/3950791 to your computer and use it in GitHub Desktop.
pymong: pymongo
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment