Added $setOnInsert support for upserts (#308)

Upserts now possible with just query parameters (#309)
This commit is contained in:
Ross Lawley
2013-05-07 11:11:55 +00:00
parent 52c162a478
commit 870ff1d4d9
4 changed files with 28 additions and 5 deletions

View File

@@ -427,7 +427,7 @@ class QuerySet(object):
.. versionadded:: 0.2
"""
if not update:
if not update and not upsert:
raise OperationError("No update parameters, would remove data")
if not write_concern:

View File

@@ -24,7 +24,8 @@ MATCH_OPERATORS = (COMPARISON_OPERATORS + GEO_OPERATORS +
STRING_OPERATORS + CUSTOM_OPERATORS)
UPDATE_OPERATORS = ('set', 'unset', 'inc', 'dec', 'pop', 'push',
'push_all', 'pull', 'pull_all', 'add_to_set')
'push_all', 'pull', 'pull_all', 'add_to_set',
'set_on_insert')
def query(_doc_cls=None, _field_operation=False, **query):
@@ -163,7 +164,9 @@ def update(_doc_cls=None, **update):
if value > 0:
value = -value
elif op == 'add_to_set':
op = op.replace('_to_set', 'ToSet')
op = 'addToSet'
elif op == 'set_on_insert':
op = "setOnInsert"
match = None
if parts[-1] in COMPARISON_OPERATORS: