繁体   English   中英

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

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

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

我想将一个字符串附加到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]
    }
 },

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM