Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamo db query using contains operator

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?

like image 876
Amir Gur Avatar asked May 25 '17 08:05

Amir Gur


People also ask

Can conditional operations be used in a DynamoDB Query?

Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.

Can you Query in DynamoDB?

In Amazon DynamoDB, you can use either the DynamoDB API, or PartiQL, a SQL-compatible query language, to query an item from a table. With Amazon DynamoDB the Query action lets you retrieve data in a similar fashion. The Query action provides quick, efficient access to the physical locations where the data is stored.

Can you Query without sort key DynamoDB?

Can we query DynamoDB without a sort key? Yes, you can use the partition key value of the item to query data.

How do I count items in DynamoDB table?

To get the item count of a dynamodb table, you have to: Open the AWS Dynamodb console and click on your table's name. In the Overview tab scroll down to the Items summary section. Click on the Get live item count button.


Video Answer


1 Answers

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

like image 95
notionquest Avatar answered Oct 13 '22 23:10

notionquest