Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

typescript - 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?

question from:https://stackoverflow.com/questions/65944258/how-to-find-value-in-dynamodb-table-without-partition-key

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...