使用DynamoDB将数字添加到列表项

时间:2018-10-28 23:45:40

标签: amazon-dynamodb

这是我正在使用的DynamoDB表结构:

{
  "userId": "99999999-9999-9999-9999-999999999999",
  "userProfile": {
    "email": "myemail@gmail.com",
    "firstName": "1234124",
    "lastName": "123423",
  },
  "masterCards": [  
    {
      "cardId": 101000000000001,
      "cardImage": "logo.png",
      "cardName": "VipCard1",
      "cardWallet": "0xFDB17d12057b6Fe8c8c425D2DB88d8475674567"
    },
    {
      "cardId": 102000000000002,
      "cardImage": "logo.png",
      "cardName": "VipCard2",
      "cardWallet": "0xFDB17d12057b6Fe8c8c425D2DB88d8183454345"
    },
    {
      "cardId": 103000000000003,
      "cardImage": "logo.png",
      "cardName": "VipCard3",
      "cardWallet": "0xFDB17d12057b6Fe8c8c425D2DB88d8184345345"
    }
  ],

}

我正在尝试使用此Lambda函数将第一个列表项的cardId字段增加一个:

const dynamoDB = new AWS.DynamoDB({region: 'eu-central-1', apiVersion:'2012-08-10'});
const counterId="99999999-9999-9999-9999-999999999999"

        const params = {
                TableName:"FidelityCardsUsers",
                Key: {"userId":{"S":counterId}},
                UpdateExpression:"ADD #masterCards[0].#cardId :increment",
                ExpressionAttributeNames:{
                    "#masterCards": "masterCards",
                    "#cardId": "cardId"
                },
                ExpressionAttributeValues:{":increment": {"N": "1"}}
            }

        dynamoDB.updateItem(params, function(err, data) {

                        if (err) {
                            console.log('error getting counter from DynamDB: ',err)
                            callback(err);
                        } else {
                            callback(null,data)

                          }

  })  

作为回报,我仅获得一个名为"mastercards[0].cardId[0]"的新顶级属性,其值编号设置为1。

2 个答案:

答案 0 :(得分:0)

我已尝试在数组中进行递增操作,并且可以使用AWS.DynamoDB.DocumentClient()

示例:

var AWS = require(“ aws-sdk”); var docClient =新的AWS.DynamoDB.DocumentClient();

let params = {
  TableName:'tableName',
  Key: {
    'venueId': 'VENUE_002'
  },
  UpdateExpression: "ADD #walk.#coordinates[0] :increment",
  ExpressionAttributeNames: {
    '#walk': 'walk',
    '#coordinates': 'coordinates'
  },
  ExpressionAttributeValues: {
    ':increment': 1 // This is from the client
  },
  ReturnValues: 'UPDATED_NEW'
};
docClient.update(params, function (err, data) {
  if (err) {
    console.log('failure:updateShuttleDirection:failed');
    console.log(err);
  } else {
    console.log('success:updateShuttleDirection:complete');
    console.log(data);
  }
});

样本数据:

"walk": {
      "coordinates": [
       10,
       20
      ],
      "type": "Point"
    },
  

我尝试将10增加到11,并且可以正常工作

答案 1 :(得分:0)

阅读文档here,看来:

  

ADD操作只能在顶级属性上使用,不能嵌套   属性。