Search code examples
amazon-web-servicesgoamazon-dynamodbaws-sdkaws-sdk-go

How to use "BETWEEN" in aws-sdk-go Dynamodb QueryInput?


I am using aws-sdk-go for dynamodb. I need to collect some items from my DB. The filtering conditions are deviceid: xyz, time >= 10 and time <= 20.

time is the sort key for my db and deviceid is the primary key. I understand I have to use BETWEEN for achieving my goal. My Implementations are not successful and as follows:

var queryInput = &dynamodb.QueryInput{
    TableName: aws.String(dbName),
    KeyConditions: map[string]*dynamodb.Condition{
        "deviceid": {
            ComparisonOperator: aws.String("EQ"),
            AttributeValueList: []*dynamodb.AttributeValue{
                {
                    S: aws.String("xyz"),
                },
            },
        },
        "time": {
            ComparisonOperator: aws.String("BETWEEN"),
            AttributeValueList: []*dynamodb.AttributeValue{
                {
                    N: aws.String("10"),
                    N: aws.String("20"),
                },
            },

        },
    },
}

What are the mistakes I made here?


Solution

  • The time value should be given as mentioned below:-

    "time": {
                ComparisonOperator: aws.String("BETWEEN"),
                AttributeValueList: []*dynamodb.AttributeValue{
                    {
                        N: aws.String("10"),                        
                    },
                    {
                        N: aws.String("20"),                        
                    },
                },
            },