amazon-web-services - 如果 StringSet 不存在,则附加到或创建它

标签 amazon-web-services amazon-dynamodb aws-lambda

所以这应该很简单......

如果存在,我想将字符串附加到 DynamoDB 中的 StringSet,如果不存在,则创建 StringSet 属性并设置该值。如果我们可以在创建时用一个空数组初始化 StringSet,那就没问题了,但可惜我们不能。

这是我到目前为止所拥有的:

const companiesTable = 'companies';

dynamodb.updateItem({
  TableName: companiesTable,
  Key: {
    id: {
      S: company.id
    }
  },
  UpdateExpression: 'ADD socialAccounts = list_append(socialAccount, :socialAccountId)',
  ExpressionAttributeValues: {
      ':socialAccountId': {
        'S': [socialAccountId]
      }
  },
  ReturnValues: "ALL_NEW"
}, function(err, companyData) {
  if (err) return cb({ error: true, message: err });

  const response = {
    error: false,
    message: 'Social account created',
    socialAccountData
  };

  cb(response);
});

我也试过...
  UpdateExpression: 'SET socialAccounts = list_append(socialAccounts, :socialAccountId)',
  ExpressionAttributeValues: {
    ':socialAccountId': {
      S: socialAccountId
    }
  },

和...
  UpdateExpression: 'ADD socialAccounts = :socialAccountId',
  ExpressionAttributeValues: {
    ':socialAccountId': {
      S: socialAccountId
    }
  },

和...
  UpdateExpression: 'SET socialAccounts = [:socialAccountId]',
  ExpressionAttributeValues: {
    ':socialAccountId': socialAccountId
  },

和...
  UpdateExpression: 'ADD socialAccounts = :socialAccountId',
  ExpressionAttributeValues: {
    ':socialAccountId': socialAccountId
  },

在上述所有其他变体中。我傻吗? DynamoDB 是否不能对数组类型字段进行简单的写入/更新?在尝试添加或设置该字段之前,我真的必须先查找该项目以查看它是否具有该字段,因为我无法使用空数组实例化该字段吗?

最佳答案

ADD 操作处理创建/更新逻辑,但仅支持数字和集合。您正在尝试添加字符串类型“S”。您需要将此字符串包装在一个数组中并将其作为字符串集“SS”传递。您也不需要等号。
您的 UpdateExpression 和 ExpressionAttributeValues 应如下所示:

 UpdateExpression: 'ADD socialAccounts :socialAccountId',
 ExpressionAttributeValues: {
   ':socialAccountId': {
      'SS': [socialAccountId]
    }
 },

有关更新项目的更多信息,请访问 here

关于amazon-web-services - 如果 StringSet 不存在,则附加到或创建它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39009687/

相关文章:

python - 将 pandas/scikit-learn 包添加到您的项目以用于 AWS lambda 的正确方法是什么

java - Amazon SQS 长轮询不返回所有消息

amazon-dynamodb - 使用Lambda(node.js): Query key condition not supported进行DynamoDB查询

amazon-web-services - 如何在 Lambda 函数上挂载 EFS?

amazon-web-services - AWS : Is it possible to share DynamoDB items across multiple users?

node.js - 动态模块 : query with hash key only

amazon-web-services - Amazon Redshift 可以自动输入 Kinesis Firehose 吗?

amazon-web-services - 什么是 AMI ImageType?

javascript - Angular 如何使用 ng-click 从 S3 Amazon 下载文件

amazon-web-services - CloudFormation 删除更新堆栈操作上的 AWS Cognito Lambda 触发器