过滤后如何进行 DynamoDB 限制? [英] How can I do DynamoDB limit after filtering?

查看:18
本文介绍了过滤后如何进行 DynamoDB 限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下逻辑实现 DynamoDB 扫描:

I would like to implement a DynamoDB Scan with the following logic:

扫描 -> 过滤(布尔值 true 或 false) -> 限制(用于分页)

但是,我只能用这个逻辑实现扫描:

However, I have only been able to implement a Scan with this logic:

扫描 -> 限制(用于分页)-> 过滤(布尔值 true 或 false)

我怎样才能做到这一点?

How can I achieve this?

以下是我编写的实现第二个扫描逻辑的示例:

Below is an example I have written that implements the second Scan logic:

    var parameters = {
        TableName: this.tableName,
        Limit: queryStatement.limit
    };
    if ('role' in queryStatement) {
        parameters.FilterExpression = '#role = :role';
        parameters.ExpressionAttributeNames = {
            '#role': 'role'
        };
        parameters.ExpressionAttributeValues = {
            ':role': queryStatement.role
        };
    }
    if ('startKey' in queryStatement) {
        parameters.ExclusiveStartKey = { id: queryStatement.startKey};
    }

    this.documentClient.scan(parameters, (errorResult, result) => {
        if (errorResult) {
            errorResult._status = 500;
            return reject(errorResult);
        }

        return resolve(result);
    });

此代码与第二个代码一样.

This codes works like second one.

扫描 -> 限制 -> 过滤

推荐答案

DynamoDB LIMIT 的工作原理如下所述(即您帖子中的第二种方法)设计.由于它是按设计工作的,因此没有解决方案.

The DynamoDB LIMIT works as mentioned below (i.e. second approach in your post) by design. As it works by design, there is no solution for this.

LastEvaluatedKey 应该用于获取后续扫描的数据.

LastEvaluatedKey should be used to get the data on subsequent scans.

扫描 -> 限制(用于分页)-> 过滤(布尔值 true 或 false)

在请求中,将 Limit 参数设置为您希望的项目数希望 DynamoDB 在返回结果之前进行处理.

In a request, set the Limit parameter to the number of items that you want DynamoDB to process before returning results.

在响应中,DynamoDB 返回限值的范围.例如,如果您发出查询或扫描限制值为 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).

对于查询或扫描操作,DynamoDB 可能会返回如果操作未返回所有匹配项,则为 LastEvaluatedKey 值表中的项目.要获得匹配项的完整计数,请采取上一个请求中的 LastEvaluatedKey 值并将其用作下一个请求中的 ExclusiveStartKey 值.重复这个直到DynamoDB 不再返回 LastEvaluatedKey 值.

For either a Query or Scan operation, DynamoDB might return a LastEvaluatedKey value if the operation did not return all matching items in the table. To get the full count of items that match, take the LastEvaluatedKey value from the previous request and use it as the ExclusiveStartKey value in the next request. Repeat this until DynamoDB no longer returns a LastEvaluatedKey value.

这篇关于过滤后如何进行 DynamoDB 限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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