【问题标题】:How to write the parameter to update a particular property's value in an object from an array?如何编写参数以从数组中更新对象中特定属性的值?
【发布时间】:2019-09-11 15:12:19
【问题描述】:

如何根据电影数组中的标题和项目 id (123) 更新数量值

我只设法更新第一层的值,例如名称 (David),但不知道如何使用数组的附加过滤器更新第二层(电影)。

发件人:

Item:
{
   id: 123,
   name: 'David',
   movies: [
      {
         id: 1,
         title: 'The lord of the ring',
         quantity: 1
      },
      {
         id: 2,
         title: 'Star Wars',
         quantity: 1
      }
   ]
}

收件人:

Item:
{
   id: 123,
   Name: 'David',
   movies: [
      {
         id: 1,
         title: 'The lord of the ring',
         quantity: 2
      },
      {
         id: 2,
         title: 'Star Wars',
         quantity: 1
      }
   ]
}

顺便说一句,我在 node.js 中使用 aws DynamoDB 文档客户端,如果你能在更新参数中分享我是如何做到的,那就太好了。

【问题讨论】:

    标签: node.js aws-lambda documentclient


    【解决方案1】:

    没有办法更新列表中的对象而不替换它。

    您可能希望重组表以模拟关系数据模型。 AWS 对此有 some documentation

    例如,像这样创建您的表:

    aws dynamodb create-table \
      --table-name movie-table \
      --attribute-definitions AttributeName=rId,AttributeType=N AttributeName=rKey,AttributeType=S \
      --key-schema AttributeName=rId,KeyType=HASH AttributeName=rKey,KeyType=RANGE
    

    该表将具有一般命名的哈希和范围键。该脚本演示了如何构造数据并添加到“计数”中:

    const { DynamoDB } = require('aws-sdk');
    
    const client = new DynamoDB.DocumentClient({ region: 'us-east-1' });
    
    const addItem = (rId, rKey, attributes) => {
      const item = { rId, rKey };
      Object.assign(item, attributes);
      return client.put({ TableName: 'movie-table', Item: item }).promise();
    };
    
    // NOTE: this is where the count attribute gets iterated
    const addToCount = (rId, rKey) => client.update({
      TableName: 'movie-table',
      Key: { rId, rKey },
      UpdateExpression: 'ADD #count :n',
      ExpressionAttributeNames: { '#count': 'count' },
      ExpressionAttributeValues: { ':n': 1 },
    }).promise();
    
    const run = async () => {
      await addItem(123, 'USER|123', { name: 'David' });
      await addItem(1, 'MOVIE|1', { title: 'The lord of the ring' });
      await addItem(2, 'MOVIE|2', { title: 'Star Wars' });
      await addItem(123, 'COUNT|1', { count: 1 });
      await addItem(123, 'COUNT|2', { count: 1 });
      await addToCount(123, 'COUNT|1');
    };
    
    run();
    

    这是脚本运行后表格的样子:

    【讨论】:

      【解决方案2】:

      我知道这有点旧,但有办法。使用文档客户端 SDK,您可以在 UpdateExpression 中引用对象属性和数组元素。但是,您不能运行任何逻辑,因此您必须知道/假设/期望元素索引就足够了。

      例如,你可以这样做:

          let params = {
               TableName: 'your-table-name',
               Key: { id: 123 },
               UpdateExpression: 'set movies[0].quantity = :x',
               ExpressionAttributeValues: { ':x': 5 }
           };
      
           const client = AWS.DynamoDB.DocumentClient();
      
           client.update(params);
      
      

      注意:您不能将索引设为表达式属性值。您必须根据您知道必须更新的索引动态构建更新表达式。这不是一个完美的解决方案,但它可以完成工作。

      作为参考,我从这里的基本(非 DocumentClient)示例派生了这个:Adding Nested Map Attributes

      【讨论】:

        猜你喜欢
        • 2020-02-03
        • 1970-01-01
        • 2020-03-29
        • 1970-01-01
        • 2021-03-26
        • 1970-01-01
        • 1970-01-01
        • 2022-01-01
        • 2021-12-03
        相关资源
        最近更新 更多