9

My table items are of the form of

function addDoc(movie,cb){
    var params = {
        TableName: "Movies",
        Item: {
            "year":  movie.year,
            "title": movie.title,
            "info":  movie.info,
            "genres" : movie.info.genres || []
        }
    };
    docClient.put(params, function(err, data) {
        bar.tick(1)
        i++;
        cb(err);
    });
}

async.eachLimit(allMovies,50,addDoc,function (err) {
    console.log(err)
    console.log("done inserting " + i + " movies");
});

I'm running this code :

var params = {
    TableName : "Movies",
    //ProjectionExpression:"#yr, title, genres, info.actors[0]",
    KeyConditionExpression: "#yr = :yyyy and contains(genres, :g1)",
    ExpressionAttributeNames:{
        "#yr": "year"
    },
    ExpressionAttributeValues: {
        ":yyyy":1992,
        ":g1" : "Drama"
    },
    //Select : "COUNT"
};
var start = Date.now()
docClient.query(params, function(err, data) {
    if (err) {
        console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
        console.log("time elapsed :",Date.now()-start);
        console.log("Query succeeded.");

        console.log(data)

    }
});

and I'm getting this error

"Invalid operator used in KeyConditionExpression: contains"

any idea?

2 Answers 2

30

There are few things that need to be clarified here.

1) The Key attributes of DynamoDB has to be scalar data type. So, I believe the attribute genres can't be defined as SET or LIST data type

2) KeyConditionExpression - can refer to Hash and Sort key only. So, I presume the attribute genres is defined as SORT key of the table

3) contains can be used on FilterExpression on data types STRING, SET or LIST. It can't be used on KeyConditionExpression

Conclusion - Refer point 3 for straight forward answer

Sign up to request clarification or add additional context in comments.

6 Comments

any idea how to perform seaching in text? as in substring searching? ex: search for blue in blue screen error
@sid8491 You can use CONTAINS. Please note that it is a case sensitive match. FilterExpression: "contains (title, :titleVal)", ExpressionAttributeValues: { ":titleVal": "somevalue" }
@sid8491 it can be used for string as well.
but the example here showed that contains can be used on KeyConditionExpression ? docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
@MKYung I'm also totally confused by now. Official docs clearly shows that 'contains' can me used in KeyConditionExpression
|
0

Based on the SDK documentation, KeyConditionExpression supports the following expressions:

a = b — true if the attribute a is equal to the value b

a < b — true if a is less than b

a <= b — true if a is less than or equal to b

a > b — true if a is greater than b

a >= b — true if a is greater than or equal to b

a BETWEEN b AND c — true if a is greater than or equal to b, and less than or equal to c.

The following function is also supported:

begins_with (a, substr)— true if the value of attribute a begins with a particular substring.

see documentation page: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.