如果 StringSet 不存在,则追加或创建它 [英] Append to or create StringSet if it doesn't exist

查看:26
本文介绍了如果 StringSet 不存在,则追加或创建它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So this should be simple...

我想将字符串附加到 DynamoDB 中的 StringSet(如果存在),或者创建 StringSet 属性(如果不存在)并设置值.如果我们可以在创建时使用空数组初始化 StringSet,那会很好,但可惜我们不能.

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. If we could initialize the StringSet on creation with an empty array, it would be fine, but alas we can not.

这是我目前所拥有的:

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 不能对数组类型字段进行简单的写入/更新吗?在我尝试添加或设置该字段之前,我是否真的必须先查找该项目以查看它是否具有该字段,因为我无法使用空数组实例化该字段?

Among about every other variation of the above. Am I dumb? Is DynamoDB not capable of simple write/updates to an array type field? 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?

推荐答案

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

The ADD action handles the create/update logic, but only supports numbers and sets. You are trying to add a string type 'S'. You need to wrap this string in an array and pass it as a string set 'SS'. You also don't need the equals sign.
Your UpdateExpression and ExpressionAttributeValues should look like this:

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

有关更新项目的更多信息,请参阅 这里

More information about updating items can be found here

这篇关于如果 StringSet 不存在,则追加或创建它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆