Skip to content

Instantly share code, notes, and snippets.

@bchirico
Created November 21, 2013 10:51
Show Gist options
  • Save bchirico/7579648 to your computer and use it in GitHub Desktop.
Save bchirico/7579648 to your computer and use it in GitHub Desktop.
from pymongo import MongoClient
import sys
import datetime
client = MongoClient('localhost', 27017)
db = client.test
things = db.things
def using_upsert():
print "using upsert"
query = {}
try:
things.drop()
things.update({'thing': 'apple'}, {'$set': {'color': 'red'}}, upsert=True)
things.update({'thing': 'pear'}, {'color': 'green'}, upsert = True)
#things.update({'thing': 'banana', 'color':'yellow'}, upsert = True)
apple = things.find_one({'thing': 'apple'})
print "apple: ", apple
pear = things.find_one({'thing': 'pear'})
print "pear: ", pear
#banana = things.find_one({'thing': 'banana'})
#print "banana: ", banana
except:
print "Unexpected error", sys.exc_info()[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment