简体   繁体   中英

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. 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. 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. 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.

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

The error is due to that you can't use a "between" condition on a partition key, only on the sort key and attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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