【问题标题】:How to remove an object from a list in DynamoDB using Lambda如何使用 Lambda 从 DynamoDB 中的列表中删除对象
【发布时间】:2020-09-12 01:51:20
【问题描述】:

我正在尝试编写一个 Lambda 函数来从 DynamoDB 的列表中删除一个对象。在这种情况下,我将"author": "J.K. Rowling""title": "Harry Potter""userid": "041c9004" 作为参数传入。我想从books 列表中删除匹配的对象。 UpdateExpression 语句的正确语法是什么? params{} 中可能还有其他一些错误。

DynamoDB 表如下所示。 userid 是主键:

{
  "books": [
    {
      "author": "J.R.R. Tolkien",
      "title": "Lord of the Rings"
    },
    {
      "author": "J.K Rowling",
      "title": "Harry Potter"
    },
    {
      "author": "George RR Martin",
      "title": "A Song of Ice and Fire"
    }
  ],
  "isactive": true,
  "ispublic": true,
  "lastupdated": 1597690265,
  "userid": "041c9004"
}

这是 Lambda 函数:

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

exports.handler = function(event, context, callback){

    let params =  {
        ExpressionAttributeValues: {
            ":attrValue": [{ 
                "author": event.author,
                "title": event.title
            }]
        },
        ExpressionAttributeNames : {
            "#books" : "books"
        },
        Key: {
            userid: event.userid
        },
        TableName: 'Users',
        UpdateExpression: "REMOVE #books[:attrValue]", //this is incorrect
        ReturnValues:"ALL_NEW",
    };
 
    docClient.update(params, function(err,data){
        if(err) {
            callback(err, null)
        }else{
            callback(null, data)
        }
    });
}

【问题讨论】:

    标签: node.js amazon-web-services aws-lambda amazon-dynamodb


    【解决方案1】:

    这是一个很好的问题!

    不幸的是,如果您指定索引 (docs here),UpdateExpression 将只允许您从列表中选择 REMOVE

    您需要从数据库中读取项目,找到您要删除的索引,并REMOVE 那个特定索引。

    【讨论】:

    • 谢谢!我希望在不先找到索引的情况下有可能,但这种方法会很好用
    【解决方案2】:

    如果有人想知道,这里是完整的解决方案:

    const AWS = require('aws-sdk');
    const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
    
    exports.handler = function(event, context, callback){
    
        var params = {
            TableName: 'Users',
            Key: {
                userid: event.userid
            }
        };
    
        docClient.get(params, function(err, data){
            if(err) {
                callback(err,null);
            } else {
                var indexOfAuthor = data.Item.books.findIndex(i => i.author === event.author);
                console.log('The index of the author is ' + indexOfAuthor);
                var updateExpressionString = "REMOVE #books[" + indexOfAuthor + "]"
                
                let paramsdelete =  {
                    ExpressionAttributeNames : {
                        "#books" : "books"
                    },
                    Key: {
                        userid: event.userid
                    },
                    TableName: 'Users',
                    UpdateExpression: updateExpressionString,
                    ReturnValues:"ALL_NEW",
                };
        
                docClient.update(paramsdelete, function(err,data){
                    if(err) {
                        callback(err, null);
                    }else{
                        callback(null, data);
                    }
                });
                
            }
        });
    };
    

    【讨论】:

      猜你喜欢
      • 2018-10-10
      • 2017-08-13
      • 2016-12-19
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      相关资源
      最近更新 更多