为什么 DynamoDB 使用 Limit 和 FilterExpression 扫描不返回符合过滤器要求的项目? [英] Why DynamoDB scan with Limit and FilterExpression not return the items that match the filter requirements?

查看:20
本文介绍了为什么 DynamoDB 使用 Limit 和 FilterExpression 扫描不返回符合过滤器要求的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 DynamoDB 上使用限制和条件进行扫描.

I need make a scan with limit and a condition on DynamoDB.

docs 说:

在响应中,DynamoDB 返回限制值范围内的所有匹配结果.例如,如果您发出限制值为 6 且没有过滤器表达式的 Query 或 Scan 请求,则 DynamoDB 会返回表中与请求中指定的键条件匹配的前六个项目(或仅返回表中的前六个项目).没有过滤器的扫描的情况).如果您还提供了 FilterExpression 值,DynamoDB 将返回前六个中也符合过滤器要求的项目(返回的结果数将小于或等于 6).

In a response, DynamoDB returns all the matching results within the scope of the Limit value. For example, if you issue a Query or a Scan request with a Limit value of 6 and without a filter expression, DynamoDB returns the first six items in the table that match the specified key conditions in the request (or just the first six items in the case of a Scan with no filter). If you also supply a FilterExpression value, DynamoDB will return the items in the first six that also match the filter requirements (the number of results returned will be less than or equal to 6).


代码(NODEJS):

var params = {
    ExpressionAttributeNames: {"#user": "User"},
    ExpressionAttributeValues: {":user": parseInt(user.id)},
    FilterExpression: "#user = :user and attribute_not_exists(Removed)",
    Limit: 2,
    TableName: "XXXX"
};

DynamoDB.scan(params, function(err, data) {
    if (err) {
        dataToSend.message = "Unable to query. Error: " + err.message;
    } else if (data.Items.length == 0) {
        dataToSend.message = "No results were found.";
    } else {
        dataToSend.data = data.Items;
        console.log(dataToSend);
    }
});



表 XXXX 定义:

  • 主分区键:用户(编号)
  • 主排序键:标识符(字符串)
  • 索引:
    • 索引名称:RemovedIndex
    • 类型:GSI
    • 分区键:已删除(数字)
    • 排序键:-
    • 属性:全部


    在上面的代码中,如果我删除 Limit 参数,DynamoDB 将返回符合过滤器要求的项目.所以,条件还可以.但是当我用 Limit 参数扫描时,结果是空的.


    In code above, if I remove the Limit parameter, DynamoDB will return the items that match the filter requirements. So, the conditions are ok. But when I scan with Limit parameter, the result is empty.

    XXXX 表,有 5 个项目.只有 2 个第一具有 Removed 属性.当我在没有 Limit 参数的情况下扫描时,DynamoDB 返回没有 Removed 属性的 3 个项目.

    The XXXX table, has 5 items. Only the 2 firsts have the Removed attribute. When I scan without Limit parameter, DynamoDB returns the 3 items without Removed attribute.

    我做错了什么?

    推荐答案

    来自您引用的文档:

    如果您还提供 FilterExpression 值,DynamoDB 将返回前六项中也符合过滤要求的项目

    If you also supply a FilterExpression value, DynamoDB will return the items in the first six that also match the filter requirements

    通过结合 Limit 和 FilterExpression,您已告知 DynamoDB 仅查看表中的前两项,并针对这些项目评估 FilterExpression.DynamoDB 中的限制可能会令人困惑,因为它与 RDBMS 中 SQL 表达式中的 limit 的工作方式不同.

    By combining Limit and FilterExpression you have told DynamoDB to only look at the first two items in the table, and evaluate the FilterExpression against those items. Limit in DynamoDB can be confusing because it works differently from limit in a SQL expression in a RDBMS.

    这篇关于为什么 DynamoDB 使用 Limit 和 FilterExpression 扫描不返回符合过滤器要求的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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