[SERVER-10523] Lower/Upper limit for $inc Created: 15/Aug/13  Updated: 10/Dec/14  Resolved: 15/Aug/13

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.5.1
Fix Version/s: None

Type: New Feature Priority: Minor - P4
Reporter: Sam Millman Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-6566 Support conditional updates: $updates Closed
Backwards Compatibility: Fully Compatible
Participants:

 Description   

I can't believe this hasn't come up yet, I have Google searched for this but couldn't find anything.

I had a case recently where, while maintaining counters, I wanted an atomic way to ensure that a counter could not pass below 0. Of course currently I have just implemented this in the client side but it would be good to have a server-side edition of this me thinks.



 Comments   
Comment by Scott Hernandez (Inactive) [ 15/Aug/13 ]

Please see other linked and other feature requests and vote them up.

Comment by Sam Millman [ 15/Aug/13 ]

Indeed that is one way of doing it, can get a hairy for ORMs to integrate (especially with multiple $incs) with this well though I see your point.

Edit: In fact multiple $incs would be hard to do this in general.

Comment by Scott Hernandez (Inactive) [ 15/Aug/13 ]

You can do this in your query + update:

update({_id:1, count:{$gt:1}}, {$inc:{count:-1}})

Generated at Thu Feb 08 03:23:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.