user1503606
user1503606

Reputation: 4290

AWS DynamoDb querying a table

I am trying to use Dynamodb on a new project and i have to say i am very confused.

So i have setup a new table with a hash key called ID which i am setting as a timestamp as it needs to be unique.

I am then inserting my data as follow with Lambda

var tableName = "TrackerTable";
    var datetime = new Date().getTime().toString();
    var putData = {
        "TableName": tableName,
        "Item": {
            "ID" : {
                "N": datetime
            },
            "TrackIt": {
                "S": event.trackit
            }, 
            "Latitude": {
                "N": event.lat
            },
            "Longitude": {
                "N": event.lon
            },
            "Time": {
                "N": datetime
            }
        }
    }
    dynamoDB.putItem(putData, function(err, data) {
        if (err) {
            context.fail('ERROR: Dynamo failed: ' + err);
        } else {
            console.log('Dynamo Success: ' + JSON.stringify(data, null, '  '));
            context.succeed('SUCCESS');
        }
    });

So my table starts to look like this which is fine. enter image description here So i am trying to use Lambda to return my results based on the trackit column?

I have the following code which doesnt work.

var tableName = "TrackerTable";
    var datetime = new Date().getTime().toString();

    var queryData = {
        "TableName": tableName,
        "ConsistentRead": true,
        "KeyConditionExpression": "trackit = :val",
        "ExpressionAttributeValues": {":val": {"S": "05LUGFr7494"}}
    };
    dynamoDB.query(queryData, function(err, data) {
        if (err) {
            context.fail('ERROR: Dynamo failed: ' + err);
        }else{
            console.log('Dynamo Success: ' + JSON.stringify(data, null, '  '));
            context.done(null, data.Items);
        }
    });

I get this error.

{
  "errorMessage": "ERROR: Dynamo failed: ValidationException: Query condition missed key schema element: ID"
}

How can i query all the trackit values i cant add the ID value as it will only return one value?

Very confused.

Upvotes: 1

Views: 1744

Answers (1)

Squirrel
Squirrel

Reputation: 241

The way you have your table setup I think you'll need to do a scan here instead of a query[1]. If this is an operation that your will be doing frequently and want to have the ability to do a query, you would need to add the trackit property as one of your secondary indexes.

[1] http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html

Upvotes: 1

Related Questions