使用 Node Js 更新 Dynamo db 中的集合 [英] Updating a Set in Dynamo db using Node Js

查看:29
本文介绍了使用 Node Js 更新 Dynamo db 中的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在发电机数据库列表中执行最简单的操作之一更新".

I am trying to do one of the most simple operations "Update" in a dynamo db list.

表架构 -

businessId : String, customers: StringSet,  itemCode : NumberSet

我通过 put 插入了一个条目 -

I have an entry inserted via put -

bussinessId = "sampleBusiness", cuatomers 0: "cust1", itemCode 0: 4554

我想使用更新添加更多项目,这是我尝试过的 -

I want to add more items using update and here is what I have tried -

 var updateRequest = {  
    'TableName' : tableName,
    'Key' : {
        'businessId' : {
            "S" : businessId
        }
    },
    'UpdateExpression' : "SET itemCode[2] =:attrValue",    
    'ExpressionAttributeValues' : {
        ':attrValue' : {
            "N" : "564564"
        }
    }
};

这给了我错误-

文档中提供的文档路径无效

Document Path provided in document is invalid

我想追加新条目,所以也尝试了这个 -

I wanted to append new entries so tried this as well -

var sm = [];
sm[0] = "56465";

//Add business to 
var updateRequest = {  
    'TableName' : tableName,
    'Key' : {
        'businessId' : {
            "S" : businessId
        }
    },
    'UpdateExpression' : 'SET #attrName = list_append(#attrName, :attrValue)',
    'ExpressionAttributeNames' : {
        '#attrName' : 'itemCode'
    },
    'ExpressionAttributeValues' : {
        ':attrValue' : {
            "NS" : sm
        }
    }
  };

这给出了:

ValidationException: Invalid UpdateExpression: 运算符或函数的操作数类型不正确;运算符或函数:list_append,操作数类型:NS

ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: NS

也尝试过这个-

 ':attrValue' : {
                "N" : "4564"
            }

但同样的错误.

根据 http://docs 中提供的示例.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Modifying.html ,它在 FiveStar 评论列表中添加了一个新元素.表达式属性名称#prProductReviews;属性值 :r 是一个单元素列表.如果列表之前有两个元素,[0] 和 [1],那么新元素将是 [2].

As per the example provided in http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Modifying.html , it adds a new element to the FiveStar review list. The expression attribute name #pr is ProductReviews; the attribute value :r is a one-element list. If the list previously had two elements, [0] and [1], then the new element will be [2].

SET #pr.FiveStar = list_append(#pr.FiveStar, :r) 

:r 是一个元素列表

我在这里遗漏了一些东西.请求是否有人可以提供帮助.纠结这个很久了.我只想使用 nodeJS 在 dynamo db 的集合中追加元素.

I am missing some thing here. Request if any one can help. Struck on this for long time. I just want to append elements in set in dynamo db using nodeJS.

推荐答案

看起来是这样的:

'ExpressionAttributeValues' : {
    ':attrValue' : {
        "NS" : sm
    }
}

应该是这样的:

'ExpressionAttributeValues' : {
    ':attrValue' : {
        "S" : sm
    }
}

或者您需要将此值 sm[0] = "56465"; 转换为数字 Number("56465") 并更改 :attrValue 数据类型 "S""N".取决于您如何配置表.

Or you need to cast this value sm[0] = "56465"; to a Number Number("56465") and change the :attrValue data type "S" to "N". Depends on how you have your table configured.

您也可能应该将 :attrValue 分配为 "S" : sm[0] 因为现在您正在传递一个 "S" 一个完整的数组.

It's possible too that you should assign :attrValue to be "S" : sm[0] because right now you are passing an "S" a whole array.

这篇关于使用 Node Js 更新 Dynamo db 中的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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