2016-08-18 59 views
0

所以這應該是簡單的...追加或建立StringSet如果它不存在

我想追加一個字符串到StringSet在DynamoDB如果存在的話,或者如果它不創造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不能簡單地寫入/更新數組類型字段嗎?在嘗試添加或設置該字段之前,我是否真的必須首先查找該項目以查看它是否具有該字段,因爲我無法用空數組實例化該字段?

回答

1

ADD操作處理創建/更新邏輯,但僅支持數字和集合。你正試圖添加一個字符串類型'S'。你需要將這個字符串包裝在一個數組中,並將其作爲一個字符串集合'SS'傳遞。你也不需要等號。
你UpdateExpression和ExpressionAttributeValues應該是這樣的:

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

更多更新項目的信息可以發現here

+0

完美,這解決了我的問題! –

相關問題