明壁幕府忍法帳 > Aptana Index > Home > Mobile Backend Services > Mobile Backend Services How-tos > Atomic Increment Operator

2019.02.07 Ver.18 (2020.4.5)

Atomic Increment Operator

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:

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"
  }
}