Complete the code to define a Local Secondary Index (LSI) with a sort key.
LocalSecondaryIndexes: [{
IndexName: 'LSI1',
KeySchema: [
{ AttributeName: 'UserId', KeyType: 'HASH' },
{ AttributeName: [1], KeyType: 'RANGE' }
],
Projection: { ProjectionType: 'ALL' }
}]The sort key for an LSI must be different from the partition key. 'Timestamp' is commonly used as a sort key.
Complete the query to use the LSI by specifying the index name.
const params = {
TableName: 'Orders',
IndexName: [1],
KeyConditionExpression: 'UserId = :uid and Timestamp > :ts',
ExpressionAttributeValues: {
':uid': 'user123',
':ts': 1609459200
}
};To query using a Local Secondary Index, you specify its name in the 'IndexName' parameter. Here, 'LSI1' is the correct index name.
Fix the error in the LSI definition by choosing the correct attribute type for the sort key.
AttributeDefinitions: [
{ AttributeName: 'UserId', AttributeType: 'S' },
{ AttributeName: 'Timestamp', AttributeType: [1] }
],The 'Timestamp' attribute is usually a number, so its type should be 'N' (Number) in DynamoDB.
Fill both blanks to complete the LSI query with correct KeyConditionExpression and attribute values.
const params = {
TableName: 'Orders',
IndexName: 'LSI1',
KeyConditionExpression: 'UserId = :uid and [1] > :ts',
ExpressionAttributeValues: {
':uid': 'user123',
[2]: 1609459200
}
};The sort key attribute is 'Timestamp' and the placeholder in ExpressionAttributeValues must match ':ts' used in the KeyConditionExpression.
Fill all three blanks to define an LSI with correct attribute definitions, key schema, and projection type.
AttributeDefinitions: [
{ AttributeName: 'UserId', AttributeType: 'S' },
{ AttributeName: [1], AttributeType: 'N' }
],
LocalSecondaryIndexes: [{
IndexName: 'LSI1',
KeySchema: [
{ AttributeName: 'UserId', KeyType: 'HASH' },
{ AttributeName: [2], KeyType: 'RANGE' }
],
Projection: { ProjectionType: [3] }
}]The LSI uses 'Timestamp' as the sort key attribute with type 'N'. The projection type 'ALL' means all attributes are projected.