简体   繁体   English

AWS DynamoDB 扫描过滤器表达式返回空

[英]AWS DynamoDB Scan Filter Expression Returning Empty

I'm stumped trying to figure out why my scan won't return anything but [ ].我很难弄清楚为什么我的扫描除了 [ ] 什么都不会返回。 Here are my scan params:这是我的扫描参数:

var params = {
                TableName: tableName,
                FilterExpression: "#wager = :wager",
                ExpressionAttributeNames: {
                    "#wager": "wager"
                },
                ExpressionAttributeValues: {
                    ":wager": wager
                }
            };

My DynamoDB table works perfectly when I run a filter expression in the DynamoDB dashboard, like "wager [NUMBER] = 0.001".当我在 DynamoDB 仪表板中运行过滤器表达式时,我的 DynamoDB 表运行良好,例如“赌注 [NUMBER] = 0.001”。

jellycsc and Seth Geoghegan already mentioned the two most likely explanations in comments: jellycsc 和 Seth Geoghegan 已经在评论中提到了两个最可能的解释:

First, make sure you do not call a single Scan operation, but rather do a loop to get all the pages of the scan result.首先,确保您不调用单个Scan操作,而是执行循环以获取扫描结果的所有页面 The specific way to do this depends on which programing language you are using.执行此操作的具体方法取决于您使用的编程语言。 When your filter leaves only a small subset of the results (eg, only when wage is exactly 0.001) remember to read all the pages is critical, because the first page may be empty: DynamoDB might have read 1MB of items (the default page size), and none of them matched wager=0.001 so an empty first page is return.当您的过滤器只留下一小部分结果时(例如,仅当工资正好为 0.001 时)记住读取所有页面很关键,因为第一页可能是空的:DynamoDB 可能已经读取了 1MB 的项目(默认页面大小),并且没有一个匹配下注=0.001,因此返回的是空的第一页。

Second, the wager might have a wrong type.其次, wager可能有错误的类型。 Obviously, if you store numbers but search for a string, nothing would match, so check you didn't do that.显然,如果您存储数字但搜索字符串,则不会匹配任何内容,因此请检查您没有这样做。 But a more subtle problem can be how you store numbers.但一个更微妙的问题可能是您如何存储数字。 DynamoDB holds floating-point in an unusual manner - using decimal, not binary, digits. DynamoDB 以不寻常的方式保存浮点数 - 使用十进制而不是二进制数字。 This means that DynamoDB can hold the number "0.001" precisely, without any rounding errors.这意味着 DynamoDB 可以精确地保存数字“0.001”,而不会出现任何舍入错误。 The same cannot be said for most programming languages.对于大多数编程语言来说,情况并非如此。 On my machine, if I set a "double" variable to 0.001, the result is 0.0010000000000000000208.在我的机器上,如果我将“double”变量设置为 0.001,则结果为 0.0010000000000000000208。 If I pass this to DynamoDB, the equality check will not match!如果我将此传递给 DynamoDB,则相等性检查将不匹配! This means you should make sure that wager variable is not a double.这意味着您应该确保wager变量不是双倍。 In Python, for example, wager should be set to Decimal("0.001") - note how it is constructed from the string "0.001", not from the floating-point 0.001 which already has rounding errors.例如,在 Python 中, wager应设置为Decimal("0.001") - 注意它是如何从字符串 "0.001" 构造的,而不是从已经存在舍入误差的浮点 0.001 构造的。

thanks for the ideas everyone.感谢大家的想法。 it did indeed turn out to be a type issue - all I had to do was cast "wager" as这确实是一个类型问题——我所要做的就是将“赌注”作为

wager = Number(wager);赌注=数字(赌注);

ahead of setting the scan params (the same params I have in the question worked).在设置扫描参数之前(我在问题中使用的相同参数有效)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM