Here's a use case: updating view count for a page and keeping a timestamp of the first view.
So I would do something like this:
coll.update({'url': url}, {'first_viewed': now, '$inc': {'views': 1}}, upsert=True)
However, that would overwrite first_viewed for every update. Is there a modifier that is only applied when a document is new on upsert? Like
coll.update({'url': url}, {'$if_new': {'first_viewed': now}, '$inc': {'views': 1}}, upsert=True)
Or possibly a modifier to set a field only if not present? Like this:
coll.update({'url': url}, {'$setdefault': {'first_viewed': now}, '$inc': {'views': 1}}, upsert=True)
Docs suggest there are none, but this seems like something a lot of people should have encountered before me.
Thanks.
-Sergey