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

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

问题描述

我有一个 dynamoDB 表,其中包含一个包含用户 ID 和列表列表的项目.它看起来像这样:

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 的列表数组中.

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天全站免登陆