mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Document.modify not working as expected

Open Pacheco95 opened this issue 2 years ago • 1 comments

I'm sorry if I'm wrongly using this method

I'm trying to define a utility QuerySet like:

from mongoengine import QuerySet

class CommonQuerySet(QuerySet):
    def update_by_id_or_raise(self, document_id, fields: dict):
        updated_document = self.filter(id=document_id).modify(
            new=True,
            **fields
        )

        if not updated_document:
            raise ('Document not found', document_id)

        return updated_document
def update_document(id: str, doc_dto: MyModelDto) -> MyModel:
    doc_dict = dataclasses.asdict(doc_dto)

    updated_doc = MyModel.objects.update_by_id_or_raise(id, doc_dict)

    return updated_doc

But getting this error:

Traceback (most recent call last):
  File "venv/lib/python3.8/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "venv/lib/python3.8/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "venv/lib/python3.8/site-packages/maas_security/keycloak.py", line 263, in wrapper
    return context.execute_adapter(auth_result)
  File "venv/lib/python3.8/site-packages/maas_security/context.py", line 129, in execute_adapter
    return self.adapter.execute(self, auth_result)
  File "venv/lib/python3.8/site-packages/maas_security/adapter.py", line 59, in execute
    return context.protected_method.invoke()
  File "venv/lib/python3.8/site-packages/maas_security/context.py", line 57, in invoke
    return self.method(*self.args, **self.kwargs)
  File "venv/lib/python3.8/site-packages/flask_smorest/blueprint.py", line 261, in wrapper
    return func(*f_args, **f_kwargs)
  File "venv/lib/python3.8/site-packages/webargs/core.py", line 450, in wrapper
    return func(*args, **kwargs)
  File "venv/lib/python3.8/site-packages/flask_smorest/arguments.py", line 83, in wrapper
    return func(*f_args, **f_kwargs)
  File "venv/lib/python3.8/site-packages/flask_smorest/response.py", line 84, in wrapper
    func(*args, **kwargs)
  File "app/rests/commission_tax_rest.py", line 116, in update_commission_tax
    new_tax = bo.update_commission_tax(tax_id, tax)
  File "app/businesses/commission_tax_bo.py", line 37, in update_commission_tax
    updated_tax = CommissionTaxModel.objects.update_by_id_or_raise(
  File "app/db/querysets/common_query_set.py", line 17, in update_by_id_or_raise
    updated_document = self.filter(id=document_id).modify(
  File "venv/lib/python3.8/site-packages/mongoengine/queryset/base.py", line 671, in modify
    update = transform.update(queryset._document, **update)
  File "venv/lib/python3.8/site-packages/mongoengine/queryset/transform.py", line 301, in update
    field = cleaned_fields[-1]
IndexError: list index out of range

The error is happening on this line https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/queryset/transform.py#L305

I'm using mongoengine through flask-mongoengine v1.0.0

Here is a fully functional example

from mongoengine import QuerySet, StringField, IntField, Document
from mongoengine import connect

connect(host="mongodb://localhost:27017/test")

class CommonQuerySet(QuerySet):
    def update_by_id_or_raise(self, document_id, fields: dict):
        updated_document = self.filter(id=document_id).modify(
            new=True,
            **fields
        )

        if not updated_document:
            raise ('Document not found', document_id)

        return updated_document


class CommissionTaxModel(Document):
    meta= { "queryset_class": CommonQuerySet }
    type = StringField(required=True)
    trigger = StringField(required=True)
    value = IntField(required=True, min_value=0)


update = {"type": "CHANNEL", "trigger": "mmadeira", "value": 3}

updated_tax = CommissionTaxModel.objects.update_by_id_or_raise(
    "618c05ccc0a15d8f95ef101d", update
)

Pacheco95 avatar Nov 23 '21 21:11 Pacheco95

modify() cannot support update field with name type etc.

https://github.com/MongoEngine/mongoengine/pull/2672

bunnyxt avatar Jul 07 '22 00:07 bunnyxt

Fix was merged , thanks @bunnyxt

bagerard avatar Jan 09 '23 21:01 bagerard