Xin Li
Xin Li

Reputation: 129

Why dynamodb query() always say I am applying type M for BETWEEN operator?

I have get parameter like:

param = {
    TableName: 'sizings',
    KeyConditions: {
        "user" : {
            "ComparisonOperator" : "EQ",
            "AttributeValueList" : [{"S":"xxxxxxxxxxxxx"}]
        },/*
        "ts" : {
            "ComparisonOperator" : "BETWEEN",
            "AttributeValueList" : [ { N : 0 } , { N : 100 } ]
        }*/
    },
    select: 'SPECIFIC_ATTRIBUTES',
    AttributesToGet:['user']
};

But the query will return an error:

ValidationException: One or more parameter values were invalid: ComparisonOperator BETWEEN is not valid for M AttributeValue type

Why It consider i specified M? Actually I specified type N for AttributeValue parameter.

Upvotes: 2

Views: 2502

Answers (1)

notionquest
notionquest

Reputation: 39196

Here is the correct syntax using BETWEEN.

var params = {
    TableName : 'sizings',
    KeyConditions: {
        "user" : {
            "ComparisonOperator" : "EQ",
            "AttributeValueList" : [{"S":"d1"}]
        },
        "ts" : {
            "ComparisonOperator" : "BETWEEN",
            "AttributeValueList" : [ { "N" : "20170101" } , { "N" : "20171231" } ]
        }
    },
    Select: 'SPECIFIC_ATTRIBUTES',
    AttributesToGet: ['user']
};

Please note that you are using legacy parameters.

KeyConditions — (map) This is a legacy parameter. Use KeyConditionExpression instead.

Without using legacy parameters:-

var params = {
    TableName : 'tableName',
    KeyConditionExpression : 'device_id = :deviceIdVal and timestampAttr between :t1 and :t2',  
    ExpressionAttributeValues : {
        ':deviceIdVal' : 'd1',
        ':t1' : 20170101,
        ':t2' : 20171231
    }
};

Upvotes: 1

Related Questions