Query

Retrieves all items with a given partition key, optionally filtered by sort key condition. More efficient than Scan. Can target GSIs for additional access patterns.

Syntax

dynamodb
await docClient.send(new QueryCommand({ TableName, KeyConditionExpression, ExpressionAttributeValues }))

Example

dynamodb
const result = await docClient.send(new QueryCommand({
  TableName: 'MyApp',
  KeyConditionExpression: 'PK = :pk AND begins_with(SK, :sk)',
  ExpressionAttributeValues: { ':pk': 'USER#alice', ':sk': 'ORDER#' },
  ScanIndexForward: false,
  Limit: 20,
}));