使用数组值作为 FilterExpression 扫描/查询 DynamoDB 表的 AWS Lambda 函数 [英] AWS Lambda function to scan/query DynamoDB table using array values as FilterExpression

查看:20
本文介绍了使用数组值作为 FilterExpression 扫描/查询 DynamoDB 表的 AWS Lambda 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:我正在尝试使用 API 作为 KeyConditionExpression 发送的值对表(表名称 HCI.LocCatApp)进行查询,并且我将结果(必须是数字而不是字符串)存储在一个数组中,我想将此数组中的每个值用作 FilterExpression 来扫描另一个表(表名 HCI.Category) .. 所以我需要的是对数组值进行循环,将每个值作为 FilterExpression 并执行 scan 操作.我目前正在尝试使用 IN 但我不确定它是否受支持.请记住,数组是在运行时被填充的.而回调只能执行一次.

here's my case: I'm trying to make a query on a table (table name HCI.LocCatApp) using a value sent by API as KeyConditionExpression, and I'm storing the results (which must be numbers not strings) in an array, and I want to use each value from this array as a FilterExpression to scan another table (table name HCI.Category) .. So what I need is to loop on the array values, take each of them as FilterExpression and perform the scan operation. I'm currently trying to use IN but I'm not sure if it's even supported or not. And keep in mind that the array is being filled during the runtime. And the callback can be performed only once.

这是我的代码:

'use strict'

var AWS = require('aws-sdk');
var mydocumentClient = new AWS.DynamoDB.DocumentClient();

exports.handler = function (event, context, callback) {

    var params = {
        TableName: 'HCI.LocCatApp',
        KeyConditionExpression : 'LocID = :lid',
            ExpressionAttributeValues: {
            ":lid": event.LocID
        },
        ProjectionExpression: 'CatID'

    };
    var catIDs = [];
    var catIDsObject = {};
    var index = 0;

    mydocumentClient.query(params, function (err, data){
        if (err) {
            callback(err, null);
        }else{
          data.Items.forEach(function(item){catIDs.push(item.CatID)});
          //callback(null, catIDs);

        }
    })

    catIDs.forEach(function(value){
      index ++;
      var catIDsKey = ":catID"+index;
      catIDsObject[catIDsKey] = value;
    })


      var params2 = {
        TableName: 'HCI.Category',
        FilterExpression : "CatID IN (:cIDs)",
        ExpressionAttributeValues : {
          ':cIDs' : catIDs
        }

      };
      mydocumentClient.scan(params2, function (err, data){
        if (err) {
            callback(err, null);
        }else{
          callback(null, data);
        }
    })

}

由于某种原因,当前代码运行成功但没有找到任何匹配项,即使我在数组中手动​​填充值,仍然没有结果,IN操作没有't 似乎工作.非常感谢

For some reason, the current code runs successfully but it doesn't find any matches, even if I fill in the values manually in the array, there's still no results, the IN operation doesn't seem to work. And many thanks in advance

推荐答案

在你的代码中 catIds 是一个 ID 数组(可能是字符串).

In your code catIds is an array of IDs (strings probably).

当您将它传递给 FilterExpression 时,您假设它将被转换为 a) 字符串 b) 为正确格式的字符串.

When you pass it to FilterExpression, you are assuming that it will be converted to a) string b) to a string in correct format.

FilterExpression : "CatID IN (:cIDs)",
        ExpressionAttributeValues : {
          ':cIDs' : catIDs
        }

我目前无法自己尝试,但我假设这是查询失败的地方.IN 运算符需要一个逗号分隔的值列表,以在括号中进行比较.所以,插入数组进行查询后,应该是这样的

I cannot try this myself at the moment, but I'm assuming this is where the query fails. IN operator expects a comma separated list of values to compare to, in parenthesis. So, after the array is inserted to query, it should be like this

FilterExpression : "CatID IN (cat1, cat2, cat2)",

但很可能它包含额外的一组 [],甚至可能是数组到字符串的转换导致它类似于 [Object object]

But most probably it contains extra set of [ and ], and maybe even the array to string conversion causes it to something like [Object object] etc.

一种解决方案是使用 Array.join 将数组中的所有元素连接成单个字符串,然后将其传递给 FilterExperession.像这样

One solution would be to use Array.join to concatenate all the elements from the array to single string before passing it to FilterExperession. Something like this

 FilterExpression : "CatID IN (:cIDs)",
            ExpressionAttributeValues : {
              ':cIDs' : catIDs.join()
            }

这篇关于使用数组值作为 FilterExpression 扫描/查询 DynamoDB 表的 AWS Lambda 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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