MongoEngine / MongoEngine/mongoengine

Using operator names in field names when updating

Open
#1,032 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

Re: https://github.com/MongoEngine/mongoengine/issues/949, https://github.com/MongoEngine/mongoengine/issues/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}}}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The entry point is mongoengine.queryset.transform.update; start by reproducing the shown Human and Shoes calls and reading how MATCH_OPERATORS and UPDATE_OPERATORS are handled. Compare the related issues 949 and 843, then establish regression coverage for type, shoes__size, and age__gt so the intended update mapping and error behavior are explicit.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.