[SERVER-28789] Add a 'initial value' parameter to $inc Created: 13/Apr/17  Updated: 06/Dec/22  Resolved: 29/Jun/19

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Edem Nsefik Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Do Votes: 1
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
is related to SERVER-2643 Allow Field Name Duplication with Mod... Closed
Assigned Teams:
Query
Participants:

 Description   

$inc is great to increment an existing field by a given amount, however for a newly created document you should be able to set the initial value for the field, rather than to assume it should be the same as the increment value.

Change

{ $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } } 

To

{ $inc: { <field1>: {start:<value1>, next:<amount1},
             <field2>:  {start:<value2>, next:<amount2}, ... } } 

This need became apparent when using upsert = true on an update with a field that is normally incremented if it exists, but should have a user defined starting value when it does not.



 Comments   
Comment by Asya Kamsky [ 29/Jun/19 ]

SERVER-40381 implemented support for aggregation expressions to specify update for 4.2. 

You can see some examples here.

This can be done by testing if the field that you want to increment/etc exists in the document and take appropriate action based on result.

Comment by Asya Kamsky [ 15/May/17 ]

This feature would be useful not just for upserts, but for regular updates as well, since it's possible for a particular field not to exist when it's first being updated via $inc.

In addition it makes sense to have this option for update operators other than "$inc" - for instance it makes sense for $push, $min $max, etc. Currently some update operators that expect numbers start with initial value of 0 for empty fields($inc,$mul,$bit), others use nothing($min, $max), array operators assume arrays are [].

The new feature should be considered in the context of all of these operators accepting optional initial value other than default. Since that syntax might get complex, it's possible that instead, the same requirement could be satisfied with ordered conditional updates feature tracked in SERVER-6566.

Comment by David Storch [ 13/Apr/17 ]

Thanks for the feature request ensefik. I am sending this to the MongoDB Query Team for consideration.

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