簡體   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