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

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

问题描述

我需要对DynamoDB进行限制和条件扫描。

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

docs 说:


在响应中,DynamoDB返回在Limit值范围内的所有匹配结果。例如,如果您发出限制值为6且没有过滤表达式的查询或扫描请求,则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

    • 分区键:已删除(数字)

    • 排序键:-

    • 属性:ALL

    • Primary partition key: User (Number)
    • Primary sort key: Identifier (String)
    • INDEX:
      • Index Name: RemovedIndex
      • Type: GSI
      • Partition key: Removed (Number)
      • Sort key: -
      • Attributes: ALL


      在上面的代码中,如果我删除了 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个头具有已移除属性。当我扫描时没有 Limit 参数时,DynamoDB将返回3个没有 Removed 属性的项目。

      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中的Limit可能会造成混淆,因为它与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.

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

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