【问题标题】:How to scan AWS DynamoDB with wild card in Nodejs如何在 Nodejs 中使用通配符扫描 AWS DynamoDB
【发布时间】:2020-09-12 10:11:58
【问题描述】:

我在nodejs中使用aws-sdk,我应该用通配符扫描数据库。

我用aws developer guide 试过这个:

var params = {
    TableName: RecipeTable,
    FilterExpression: "#recipe = :recipe",
    ExpressionAttributeNames:{
        "#recipe": "recipe",
    },
    ExpressionAttributeValues: {
        ":recipe": request.params.recipe,
    }
};

我无法找到答案。

有人可以帮我吗?

谢谢。

【问题讨论】:

  • 在指南中他们使用KeyConditionExpression 而不是FilterExpression。你试过吗?
  • @Molda 在查询中,他们使用KeyConditionExpresion。但在扫描中,他们使用FilterExpression。 :(

标签: node.js amazon-web-services amazon-dynamodb


【解决方案1】:

我找到了答案:使用 contains 关键字。

app.get('/recipe/:recipe', (request, response) => {
    var params = {
        TableName: RecipeTable,
        FilterExpression: "contains(#recipe, :recipe)", // Here!
        ExpressionAttributeNames:{
              "#recipe": "ingredients"
        },
        ExpressionAttributeValues: {
              ":recipe": request.params.recipe
        }
    };

    result = [];
    docClient.scan(params, onScan);

    function onScan(err, data) {
        if (!err) {
            data.Items.forEach((itemdata) => {
                result.push(itemdata);
            });

            if(typeof data.LastEvaluatedKey != "undefined") {
                params.ExclusiveStartKey = data.LastEvaluatedKey;
                docClient.scan(params, onScan);
            } else {
                response.send(JSON.stringify({"result" : result}));
            }
        }
    }
})

我已经引用了this document

谢谢。

【讨论】:

  • 这是您经常进行的扫描吗?您正在扫描的数据集有多大?我问的是随着时间的推移,这可能会在成本和性能方面变得非常昂贵。
  • @Kirk 大约 100,000 行 (14.4MB)。有没有什么好的选择来代替那个代码?
  • 好吧,你永远不会说你想要做什么,所以很难说是否有更好的方法。只要知道如果您经常进行扫描并且数据集很大,那么扫描可能会很昂贵。如果您经常使用扫描,您可能需要重新评估您的数据模型。
猜你喜欢
  • 2018-08-23
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
相关资源
最近更新 更多