Mobile Backend Services supports the Mongo $inc
atomic increment operator.
$inc
You can use $inc
to increment (or decrement) a custom field by a specified value in a single update operation.
- The operator accepts positive and negative increment amounts
- It can only be applied to a single field per method call.
$inc
is specified with the following form, where fieldName
is the name of the field to update and value
is a positive (or negative) number to increment (or decrement) fieldName
by:
"fieldName" : { "$inc:" value} |
The operator must be enclosed in quotes, as shown above. The following
methods support the $inc
operator:
- Checkins.update
- CustomObjects.update
- Events.update
- Files.update
- PhotoCollections.update
- Photos.update
- Places.update
- Posts.update
- Reviews.update
- Users.update
- Statuses.update
Examples
The following example increments the score
custom field by 10 in a CustomObjects update to a custom object called family
:
The following example decrements the score
custom field by 20 in a Checkins update:
If you apply $inc to multiple fields in one Update call, you will get an error:
$ curl -b c.txt -c c.txt -X PUT -F "checkin_id=511111945554f742d300000b" -F "custom_fields={" favorite ":" play xbox 360 ", " pet ":" shark " ," score ":{$inc:10}, " age ":{$inc:10}}" { "meta" : { "status" : "fail" , "code" : 400, "message" : "$inc operation only support to increase one field once." , "method_name" : "updateCheckin" } } |