简体   繁体   English

如果不存在,则追加或创建StringSet

[英]Append to or create StringSet if it doesn't exist

So this should be simple... 所以这应该很简单......

I want to append a string to a StringSet in a DynamoDB if it exists, or create the StringSet property if it doesn't and set the value. 我想将一个字符串附加到DynamoDB中的StringSet(如果存在),或者创建StringSet属性(如果不存在)并设置该值。 If we could initialize the StringSet on creation with an empty array, it would be fine, but alas we can not. 如果我们可以在创建时使用空数组初始化StringSet,那就没问题,但是我们做不到。

Here's what I have so far: 这是我到目前为止所拥有的:

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);
});

I've also tried... 我也试过......

  UpdateExpression: 'SET socialAccounts = list_append(socialAccounts, :socialAccountId)',
  ExpressionAttributeValues: {
    ':socialAccountId': {
      S: socialAccountId
    }
  },

and... 和...

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

and... 和...

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

and... 和...

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

Among about every other variation of the above. 在上述的每一个变化中。 Am I dumb? 我笨吗? Is DynamoDB not capable of simple write/updates to an array type field? DynamoDB不能简单地写入/更新数组类型字段吗? Do I REALLY have to lookup the item first to see if it has that field before I try to either add or set that field, because I can't instantiate that field with an empty array? 在尝试添加或设置该字段之前,我是否真的必须首先查找该项以查看它是否具有该字段,因为我无法使用空数组实例化该字段?

The ADD action handles the create/update logic, but only supports numbers and sets. ADD操作处理创建/更新逻辑,但仅支持数字和集。 You are trying to add a string type 'S'. 您正在尝试添加字符串类型“S”。 You need to wrap this string in an array and pass it as a string set 'SS'. 您需要将此字符串包装在一个数组中,并将其作为字符串集'SS'传递。 You also don't need the equals sign. 你也不需要等号。
Your UpdateExpression and ExpressionAttributeValues should look like this: 您的UpdateExpression和ExpressionAttributeValues应如下所示:

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

More information about updating items can be found here 有关更新项目的更多信息,请访问此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果新分区不存在,如何修改 CTAS 查询以将查询结果附加到表中? - 雅典娜 - How to modify CTAS query to append query results to table based on if new partition doesn't exist? - Athena 如果不存在,则使用 Cloudformation 创建 DynamoDB 全局表 - Create the DynamoDB global Table if doesn't exist using Cloudformation CDK 仅在不存在时创建新的托管策略 - CDK create new managed policy only if doesn't exist AWS DynamoDB创建更新表达式-如果不存在则添加新的字符串集 - AWS DynamoDB create update expression - Add new stringset if none exists 实例ID不存在 - Instance ID doesn't exist MySQL“表不存在”,但实际上确实存在 - MySQL “Table doesn't exist”, but it actually does 具有此 ID 的 Terraform 资源不存在 - Terraform resource with this ID doesn't exist aws resourcegroupstaggingapi 检索不存在的资源 - aws resourcegroupstaggingapi retrieving resource that doesn't exist 仅当对象不存在时,DynamoDBMapper才会保存 - DynamoDBMapper save only if object doesn't exist Aws XRay .net控制台应用程序中的“实体在CallContext中不存在” - Aws XRay “Entity doesn't exist in CallContext” in .net console application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM