[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: |
|
||||||||||||
| 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
To
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 ] |
|
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 |
| Comment by David Storch [ 13/Apr/17 ] |
|
Thanks for the feature request ensefik. I am sending this to the MongoDB Query Team for consideration. |