gpt4 book ai didi

amazon-dynamodb - DynamoDB - 读取一个项目并返回一个数组大小

转载 作者:行者123 更新时间:2023-12-05 05:22:17 29 4
gpt4 key购买 nike

我在 DynamoDB 表中有一个项目。该项目看起来像这样:

{
data: [ 1, 2, 3, 4, 5, 6 ]
more_data: [ 2, 3, 4, 5, 6, 7 ]
}

使用 ProjectionExpression,我可以执行 GetItem 并且只返回 data,或者返回 data[0]。但是有两件事我想不通:

  • 如何返回数组大小? data.SIZE ?
  • 如何返回数组的子集? 数据[2-10]

ProjectionExpression 文档似乎没有涵盖这一点。有什么线索吗?

最佳答案

1) DynamoDB 没有获取List 属性大小的函数。可以在客户端使用 length 函数找到数组元素的大小。

data.Item.records.length 给出数组元素records 的长度。 records 是 DynamoDB 表中的列表。

docClient.get(params, function(err, data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
console.log(data.Item[0].records.length);
}
});

2) 注册子数组,语法应该是这样的。 DynamoDB 不支持像 'records[1-3]' 这样的范围。

ProjectionExpression: 'records[0], records[1]',

3) DynamoDB 没有获取列表中最后一项的功能。但是,您可以在客户端获取它。我有 records 列表。我使用 itemValue.records[itemValue.records.length - 1]

得到了最后一个元素
docClient.query(params, function(err, data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err,
null, 2));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));

data.Items.forEach(function(itemValue) {
console.log (itemValue.records[itemValue.records.length - 1]);
});
}
});

关于amazon-dynamodb - DynamoDB - 读取一个项目并返回一个数组大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40882426/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com