Given a DynamoDB table with a Global Secondary Index (GSI) named GSI1 having category as the partition key, what will be the output of the following query?
const params = {
TableName: 'Products',
IndexName: 'GSI1',
KeyConditionExpression: 'category = :cat',
ExpressionAttributeValues: { ':cat': { S: 'Books' } }
};
// Assume the table has these items:
// { id: '1', category: 'Books', title: 'Learn SQL' }
// { id: '2', category: 'Electronics', title: 'Smartphone' }
// { id: '3', category: 'Books', title: 'DynamoDB Guide' }
// Query is executed with these params.What items will be returned?
const params = {
TableName: 'Products',
IndexName: 'GSI1',
KeyConditionExpression: 'category = :cat',
ExpressionAttributeValues: { ':cat': { S: 'Books' } }
};Remember, querying a GSI with a partition key returns all items matching that key in the index.
The query filters items where category equals 'Books' in the GSI. Two items match this condition.
Which statement about querying a Global Secondary Index (GSI) in DynamoDB is true?
Think about how DynamoDB replicates data to GSIs.
GSIs provide eventually consistent reads by default because data replication to GSIs is asynchronous.
Which of the following DynamoDB query parameter objects correctly queries a GSI named GSI2 with partition key category equal to 'Books' and sort key price less than 20?
Check the format of ExpressionAttributeValues for DynamoDB SDK.
ExpressionAttributeValues must specify the data type (S for string, N for number) as objects. Option B correctly uses this format.
You want to reduce the amount of data returned by a GSI query to only the id and title attributes. Which parameter should you add to your query?
Think about how to specify which attributes to return in DynamoDB queries.
ProjectionExpression specifies which attributes to return, reducing data size and improving performance.
A developer runs this query on a DynamoDB table with a GSI named GSI1:
const params = {
TableName: 'Orders',
IndexName: 'GSI1',
KeyConditionExpression: 'orderDate = :date',
ExpressionAttributeValues: { ':date': { S: '2023-01-01' } }
};But the query returns no items, even though there are orders on that date. What is the most likely cause?
Check the GSI's key schema and what the query condition uses.
The query uses orderDate as the partition key condition, but if the GSI's partition key is different, the query will return no results.