简体   繁体   English

如何查询 DynamoDB 中任何项目的 GSI 范围(即不依赖于分区键)?

[英]How do I query the range of a GSI of any item in DynamoDB (i.e. not partition key dependent)?

I am using Node.js with DynamoDB.我在 DynamoDB 中使用 Node.js。 I want to fetch the items in the table that are from the past month.我想获取过去一个月表中的项目。 I have a date GSI and the id is also linked to the date.我有一个日期 GSI,ID 也与日期相关联。 I don't want to use scan because this table will grow.我不想使用扫描,因为这个表会增长。 The main issue is that it is not item dependant, and query needs the item partition key which doesn't make sense in my case.主要问题是它不依赖于项目,并且查询需要项目分区键,这在我的情况下没有意义。

I have tried querying using the GSI on its own and querying the range with just the partition key and with the GSI on it's own.我尝试过单独使用 GSI 进行查询,并仅使用分区键和它自己的 GSI 来查询范围。 I don't know how to get this right.我不知道如何解决这个问题。

                        TableName: interactionsTable,
                        IndexName: "interactionDate",
                        KeyConditionExpression: "interactionDate between :fDay and :lDay",
                        ExpressionAttributeValues: {                           
                            ":fDay": firstDayStr,
                            ":lDay": lastDatStr
                        },
                    }

I get an error saying that the Key Condition Expression is invalid.我收到一条错误消息,指出关键条件表达式无效。 Is there a better way to address this problem?有没有更好的方法来解决这个问题?

You need to configure your GSI in another way.您需要以另一种方式配置 GSI。

Choose another field as the partition key and set interactionDate as a regular attribute for the GSI, then use a filtered query to use between in interactionDate选择另一个字段作为分区键并将interactionDate设置为GSI 的常规属性,然后使用过滤查询在interactionDate之间使用

The error is due to that you can't use a "between" condition on a partition key, only on the sort key and attributes.该错误是由于您不能在分区键上使用“介于”条件,只能在排序键和属性上使用。

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

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