Skip to content

Using operator names in field names when updating #1032

@starsirius

Description

@starsirius

Re: #949, #843

Question 1

Similar to using operator names in field names when querying, it also raises exceptions when updating.

from mongoengine import *
from mongoengine.queryset import transform

class Shoes(EmbeddedDocument):
    size = FloatField()     # `size` is in MATCH_OPERATORS
    brand = StringField()   # `brand` is not

class Human(Document):
    age = IntField()
    type = StringField()    # `type` is in MATCH_OPERATORS
    shoes = EmbeddedDocumentField(Shoes)

print transform.update(Human, type='introversion')  # Raises an "IndexError: list index out of range" error
print transform.update(Human, shoes__brand='NIKE')  # Correct. {'$set': {'shoes.brand': 'NIKE'}}
print transform.update(Human, shoes__size=10.5)  # Raises an "AttributeError: 'float' object has no attribute 'get'" error

I believe it is due to the same issue above. We can do similar trailing __ tricks as the PR, something like

transform.update(Human, type__='introversion')
transform.update(Human, shoes__size__=10.5)

BUT, more importantly, I wonder if we ever need operators, when updating, other than UPDATE_OPERATORS (e.g. type and size in the above case). See Question 2 below. Or maybe I misunderstood anything.

Question 2: operators other than UPDATE_OPERATOR when updating

Given the document classes above, I wonder the following result would be desired.

print transform.update(Human, age__gt=10)  # {'$set': {'age': {'$gt': 10}}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions