Created
November 28, 2012 22:24
-
-
Save erikbeebe/4165140 to your computer and use it in GitHub Desktop.
pymongo / card stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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