0

I want to increment the values of items in a MAP attribute called provinceDistribution in my Dynamodb table during aggregation in a lambda function, based on the province field of a user record.

Table model:

type submissionsAggregate @model {
  id: String
  totalRecords: Int
  provinceDistribution: provinces
  status: employmentStatus
  females: Int
  males: Int
}
type provinces {
  Sichuan: Int
  Qinghai: Int
  Gansu: Int
  Heilongjiang: Int
  Yunnan: Int
  Hunan: Int
  Shaanxi: Int
  Hebei: Int
  ...
}

User record:

   {
     "Timestamp": {
        "S": "2016-11-18:12:09:36"
      },
      "province": {
        "S": "Sichuan"
      },
      "gender": {
        "S": "male"
      },
      "employmentStatus": {
        "S": "employed"
      }
   }

I extracted the province field value and stored it in the ExpressionAttributeValues, hoping to create a path to the targeted item in the MAP(#province) during update like so

const params = {
    ExpressionAttributeNames: {
      "#total": "totalRecords",
      // "#status": "status",
      "#males": "males",
      "#females": "females",
      "#select": "Sichuan",
      "#province": "provinceDistribution",
    },
    ExpressionAttributeValues: {
      ":inc": 1,
      // ":targetStatus": `#status.${employmentStatus}`,
      ":targetProvince": `#province.${province}`,
      ":maleIncrement": maleIncrement,
      ":femaleIncrement": femaleIncrement,
    },
    Key: {
      id: "statistics1",
    },
    ReturnValues: "ALL_NEW",
    TableName: process.env.TABLE_NAME,
    UpdateExpression:
      "set #province=:targetProvince + :inc, #total=#total+ :inc, #males =#males+:maleIncrement, #females=#females+ :femaleIncrement",
  };

Then I get this error:

'Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: +, operand type: S',

Kind of the main issue is knowing how to traverse the Map dynamically. Say provinceDistribution.Sichuan, where "Sichuan" is the province extracted from the user record in order to increment its value in the Map.

Any suggestions on how to achieve this would be very much appreciated.

mbappai
  • 557
  • 1
  • 5
  • 22

1 Answers1

0

Your issue is set #province=:targetProvince + :inc. You are trying to concatenate a number with a string, which is not supported by UpdateExpressions https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html

Antonin Riche
  • 548
  • 6
  • 10