【问题标题】:How can I do an updateItem in a nested element in dynamodb?如何在 dynamodb 的嵌套元素中执行 updateItem?
【发布时间】:2020-08-10 12:14:22
【问题描述】:

我有存储在 dynamodb 中的地图结构,我想在学校对象中添加另一个属性

类似:

{
  name: 'Felipe'
  uid: 112233,
  data: {
    structure: {
      school: {
        name: 'beta'
      }
    }      
  }
}

以前 add_year 不是结构的一部分,所以这部分是新的

school: {
  name: 'beta'
  add_year: '2020'
}

我怎样才能做到这一点?

我尝试了以下解决方案,但没有成功

(async ()=>{
  try {
    let teste =  await dynamoDb.updateItem({
      TableName: 'test',
      Key: { 
        uid: "112233"
      },
      UpdateExpression: "SET data.#structure.#school = list_append(#structure, :attrValue)",
      ExpressionAttributeNames: {
        "#data": "data",
        "#structure": "structure",
        "#school": "school",
      },
      ExpressionAttributeValues: {
        ":school":{  
          "add_year": 2020
        }
      },
      ReturnValues:"UPDATED_NEW "
    })
    console.log('update',teste)
  } catch (error) {
    console.log(error)
  }

【问题讨论】:

    标签: javascript node.js amazon-dynamodb dynamodb-queries dynamo-local


    【解决方案1】:
    const { DocumentClient } = require('aws-sdk/clients/dynamodb');
    
    const documentClient = new DocumentClient({
      region: 'us-east-1',
    });
    
    try {
        let add_year= '2020'
        let teste = documentClient.update({
          TableName: 'test',
          Key: { 
            uid: "112233"
          },     
          UpdateExpression: `SET #data.structure.school=  :add_year`,
          ExpressionAttributeValues: {
            ':add_year':  add_year
          },
          ExpressionAttributeNames: {
            "#data": "data"   
          },
          ReturnValues:"ALL_NEW"
        }).promise()
    
        teste.then(t => console.log(t));   
    
      } catch (error) {
        console.log(error)
      }
    

    【讨论】:

      【解决方案2】:

      Felipe,你看到关于这个话题的AWS documentation了吗?

      我认为这段代码可以为你工作:

      (async () => {
          try {
              var params = {
                  TableName: 'test',
                  Key: {
                      "uid": "112233"
                  },
                  UpdateExpression: "SET data.structure.school.add_year = :year)",
                  ExpressionAttributeValues: {
                      ":add_year": 2020
                  },
                  ReturnValues: "UPDATED_NEW"
              }
      
              dynamoDb.update(params, (err, data) => {
                  if (err) {
                      console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
                  } else {
                      console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
                  }
              })
          } catch (error) {
              console.log(error)
          }
      })
      

      【讨论】:

      猜你喜欢
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多