andrey.shedko
andrey.shedko

Reputation: 3238

How to find value in DynamoDb table without partition key?

I'm trying to find values in DynamoDb table by field value, without partition key, this is how I'm trying to achieve this:

const tag = await database.query({
      TableName: 'blog_tags',
      KeyConditionExpression: 'tagName = :name',
      ExpressionAttributeValues: {
        ':name': {S: 'test'}
   }
});

But I'm getting the error ValidationException: Query condition missed key schema element: tagId

In my table, I have tagId as the partition key and tagName as the value field. But I want to search by tagName only.

How should I modify this query?

Upvotes: 0

Views: 5695

Answers (1)

Seth Geoghegan
Seth Geoghegan

Reputation: 5747

DynamoDB gives us two ways to fetch data: query and scan.

The query operation requires you to specify the primary key. The scan operation lets you fetch items by specifying any attribute.

Therefore, if you want to fetch data form DynamoDB without using the primary key, you can use the scan operation. However, be careful when using scan. From the docs:

The Scan operation returns one or more items and item attributes by accessing every item in a table or a secondary index.

The scan operation can be horribly inefficient if not used carefully. If this access pattern is common in your application, you may want to reorganize your data such that the tagName is part of the primary key. This will allow you to use the query operation to fetch the data you need.

Upvotes: 5

Related Questions