如何使用文档客户端更新dynamodb中的嵌套列表数据 [英] How do I update nested list data in dynamodb using document client

查看:124
本文介绍了如何使用文档客户端更新dynamodb中的嵌套列表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dynamoDB表,该表具有一个包含UserId和列表列表的项目。看起来像这样:

I have a dynamoDB table that has an Item that includes a UserId and a List of lists. It looks like this:

Item: 
{
    UserId: 'abc123',
    Lists: [
        {
            id: 1,
            title: 'My favorite movies',
            topMovies: [
                {
                    id: 1,
                    title: 'Caddyshack'
                },
                {
                    id: 2,
                    title: 'Star Wars'
                }
            ]

        }
    ]
}

现在,让用户拥有创建了一个名为我最喜欢的电视节目的新列表,并希望将其插入ID为2的Lists数组中。

Now, lets the user has created a new list titled, "My favorite TV Shows", and wants to insert it into the Lists array with id: 2.

如何使用文档更新此对象客户。我浏览了几个示例,但没有发现任何可以解释我要尝试做的事情。这让我觉得也许我没有正确使用DynamoDB,我应该使用不同的对象架构。

How would I update this object using document client. I've looked through several examples and I've found nothing that explains what I'm trying to do. It's making me think that perhaps I'm not using DynamoDB correctly and I should have a different object schema.

我尝试使用它,但是它会覆盖我以前的对象。

I've attempted using this, but it is overwriting my previous object.

exports.handler = (event, context, callback) => {
console.log(event);
const params = {
    TableName: "top-ten",
    Key: {
        "UserId": 'abc123',
    },
   UpdateExpression: "set Lists =:newItem",
   ExpressionAttributeValues: {
        ":newItem": {
            "id": 2,
            "title": "Favorite TV Shows",
            "topMovies": [{"id": 1, "title" : "The Simpsons"}]

        },
    },
    ReturnValues: "UPDATED_NEW"
};
dynamodb.update(params, function(err, data) {
    if (err) {
        console.log(err);
        callback(err);
    } else {
        console.log(data);
        callback(null, data);
    }
});

};

编辑:好的,我发现如果我放进去

Ok, I've figured out that if I put

 UpdateExpression: "set Lists[1] =:newItem" 

它可以正确更新项目。但是现在,我如何知道列表数组中有多少个项目?

it updates the item correctly. But now, how do I know how many items I have in my list array?

推荐答案

您应使用 list_append 。该函数将两个列表一起添加,因此您需要使您的商品添加列表。

You should use list_append. The function adds two lists together, so you need to make your item to add a list.

exports.handler = (event, context, callback) => {
console.log(event);
const params = {
    TableName: "top-ten",
    Key: {
        "UserId": 'abc123',
    },
UpdateExpression : "SET #attrName = list_append(#attrName, :attrValue)",
ExpressionAttributeNames : {
  "#attrName" : "Lists"
},
ExpressionAttributeValues : {
  ":attrValue" : [{
            "id": 2,
            "title": "Favorite TV Shows",
            "topMovies": [{"id": 1, "title" : "The Simpsons"}]

        }]
},
ReturnValues: "UPDATED_NEW"
};
dynamodb.update(params, function(err, data) {
    if (err) {
        console.log(err);
        callback(err);
    } else {
        console.log(data);
        callback(null, data);
    }
});

这篇关于如何使用文档客户端更新dynamodb中的嵌套列表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆