Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to clamp while decrementing #133

Open
TheOddler opened this issue Aug 23, 2022 · 0 comments
Open

How to clamp while decrementing #133

TheOddler opened this issue Aug 23, 2022 · 0 comments

Comments

@TheOddler
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant