Skip to content

dec atomic operation on Float/Decimal/Int Field is broken #2339

Closed

Description

@ajdas-code

It seems that the mongoengine is broken for atomic dec operation a FloatField or DecimalField.

Here is what i am trying -

class Account (DynamicDocument): 
    user = ReferenceField(User,passthrough=True,reverse_delete_rule=True,required=True,unique=True)
    amount = FloatField(min_value=0,required=True)

Now I am trying to perform a atomic "dec" operation. The "amnt" value is passed as 10

Account.objects(user=usr).update(dec__amount=amnt)

However, I am getting error - mongoengine.error.ValidationError: Float value is too small. Any help is greatly appreciated. Thanks

Activity

  1. bagerard commented on Jun 20, 2020

    @bagerard
    Collaborator

    Good catch, its taking the min_value into account to validate the value but it shouldn't in case of inc/dec

    As a workaround, you can use the __raw__ update, it will bypass the mongoengine validation:
    Account.objects(user=usr).update(__raw__={"$inc": {"amount": -amnt}})

  2. changed the title [-]dec atomic operation on floating field or decimal field is broken[/-] [+]dec atomic operation on Float/Decimal/Int Field is broken[/+] on Jun 20, 2020
  3. shalabhsingh commented on Oct 15, 2020

    @shalabhsingh

    This still seems to be broken. Just stumbled on the same error myself for Int field.

    Can this be resolved? Thanks

  4. shuheng-liu commented on Jan 14, 2021

    @shuheng-liu

    Dear all. I'm also experiencing this issue. The thread seems to have been closed but the bug persists in the latest version v0.22.1.
    I was wondering if there is any plan on a fix? Thanks in advance.

  5. kientuong114 commented on Feb 18, 2021

    @kientuong114

    The issue is that the validation in IntField is incorrect: when validating, the comparison done is the following:

    if self.min_value is not None and value < self.min_value:
        self.error("Integer value is too small")

    However, here value is the argument of dec, which means that for a min_value=0, this will fail for any decrement.
    I believe the problem lies in the fact that this is the wrong validation function to use in this context.

    The same issue lies with the validation for the max_value: if someone were to say that max_value=0 for a field containing only non-positive integers, then any inc command will fail.

    I do not believe that the validation should be skipped for either inc or dec, but the validation should be done by taking into account the current value of the field, as well as the increment/decrement, although I'm not sure if this is feasible in the current framework (I'm fairly new to mongoengine, so perhaps I'm wrong on this).

  6. sriharan16 commented on Nov 17, 2022

    @sriharan16

    @bagerard your workaround is working well but when can we expect some fix for this? This was raised more than a couple of years back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions