简体   繁体   English

如何使用文档客户端更新dynamodb中的嵌套列表数据

[英]How do I update nested list data in dynamodb using document client

I have a dynamoDB table that has an Item that includes a UserId and a List of lists. 我有一个dynamoDB表,该表具有一个包含UserId和列表列表的项目。 It looks like this: 看起来像这样:

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

        }
    ]
}

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. 现在,让用户创建一个名为“我最喜欢的电视节目”的新列表,并将其插入ID为2的Lists数组中。

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. 这让我觉得也许我没有正确使用DynamoDB,我应该拥有不同的对象架构。

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);
    }
});

}; };

EDIT: 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? 但是现在,我如何知道列表数组中有多少个项目?

You should use list_append . 您应该使用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);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM