Skip to content

Instantly share code, notes, and snippets.

@neilkod
Created July 24, 2012 02:37
Show Gist options
  • Save neilkod/3167664 to your computer and use it in GitHub Desktop.
Save neilkod/3167664 to your computer and use it in GitHub Desktop.
upsert and preserve existing values?
for itm in itms:
data = itm
rec=coll.find({'text':data})
if rec.count() == 0:
print "new item: {0}".format(itm.strip())
coll.insert({'text':data,'count':0,'last_posted': OLD_DATE})
can the coll.find/coll.insert be rewritten to use upsert (coll.update) **and preserve any existing values of count(integer) and last_posted(date)**?
each document in the collection will be text, count, last_posted .
text: some arbitrary text
count: an incremented count
last_posted: a date, initialized to an date in 1900.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment