Skip to content

Instantly share code, notes, and snippets.

@erikbeebe
Created November 28, 2012 22:24
Show Gist options
  • Save erikbeebe/4165140 to your computer and use it in GitHub Desktop.
Save erikbeebe/4165140 to your computer and use it in GitHub Desktop.
pymongo / card stuff
import bson
import pprint
from pymongo import Connection
## Connect to MongoDB
connection = Connection("localhost", 27017, safe=True)
## Authenticate
db = connection['admin']
db.authenticate('user', 'password')
## Select the "users" database
db = connection['users']
## Define a simple JSON document
doc = {'login': 'bob', 'password': 'secret'}
## Insert this document into the "accounts" collection
try:
db.accounts.insert(doc)
except Exception, ex:
print "Unable to insert, exception is: %s" % ex
## Find our user and store the returned document to variable "a"
user = db.accounts.find_one({'login': 'bob'})
## Pretty-print JSON document returned from MongoDB
pprint.pprint(user)
## Remove our user's document by _id
db.accounts.remove({"_id": bson.objectid.ObjectId(user['_id'])})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment