Skip to content

How to clamp while decrementing #133

Open
@TheOddler

Description

@TheOddler

I have a document where I want to decrement a field by 1, but it should never go negative. However, I can't figure out how to do this.

I tried two things (and a bunch of variations on this):

      findAndModifyOpts
        ( select ...)
        FamUpdate
          { famUpdate =
              [ "$inc" =: ["count" =: ((-1) :: Int)],
                "$max" =: ["count" =: (0 :: Int)]
              ],
            famNew = False,
            famUpsert = False
          }

and

      findAndModifyOpts
        ( select ...)
        FamUpdate
          { famUpdate =
              [ "$set" =: 
                  [ "count" =:
                    [ "$max" =:
                      [ "$max" =: [Int32 0, Int32 0],
                        "$subtract" =: [String "$count", Int32 1]
                      ]
                    ]
                  ]
              ],
            famNew = False,
            famUpsert = False
          }

But both end up giving a document not found error.

How do I properly do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions