David Alsh
David Alsh

Reputation: 7609

Having difficulty with DynamoDB query

I'm trying to wrap my head around AWS DynamoDB and having trouble getting records. If my db item looks like this:

{
    "id": string (primary)
    "identifier": string
    "project": string
}

I'm trying to get a record using both identifier and project: Right now I'm doing that like this:

const params = {
    TableName: 'MY_TABLE',
    ExpressionAttributeValues: {
        ':identifier': { S : 'Dave' },
        ':project': { S : 'red_squad' },
    },
    KeyConditionExpression: 'identifier = :identifier and project = :project'
}
docClient
    .query(
        params, 
        (err, data) => console.log(err || data)
    )

However it's telling me that it needs the primary key, however I that's not suitable for my use case.

Upvotes: 1

Views: 93

Answers (1)

F_SO_K
F_SO_K

Reputation: 14809

Use a scan, not a query https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#scan-property

const params = {
    TableName: 'MY_TABLE',
    ExpressionAttributeValues: {
        ':identifier': { S : 'Dave' },
        ':project': { S : 'red_squad' },
    },
    FilterExpression: 'identifier = :identifier and project = :project'
}
docClient
    .scan(
        params, 
        (err, data) => console.log(err || data)
    )

Upvotes: 1

Related Questions