DynamoDB: Query Count (Guide w/ Code Examples)

Provided by Rafal Wilinski
Last updated: October 23rd, 2026
Select COUNT returns the number of matches in one response, not the total for the entire query. Sum Count while paginating; COUNT still consumes read capacity.
Install @aws-sdk/client-dynamodb and @aws-sdk/lib-dynamodb, use an ES module on a supported Node.js runtime, and configure an AWS Region and credentials. The examples use an existing Orders table with String keys pk and sk unless an index or timestamp key is described.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb"; const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({})); let count = 0; let lastKey; do { const page = await ddb.send(new QueryCommand({ TableName: "Orders", KeyConditionExpression: "#pk = :pk", ExpressionAttributeNames: { "#pk": "pk" }, ExpressionAttributeValues: { ":pk": "CUSTOMER#123" }, Select: "COUNT", ExclusiveStartKey: lastKey, })); count += page.Count ?? 0; lastKey = page.LastEvaluatedKey; } while (lastKey && Object.keys(lastKey).length > 0); console.log(count);
With a filter, Count counts returned matches and ScannedCount counts evaluated items. Approximate table ItemCount is a different metric and is not an exact real-time count.
Reference: AWS Query API. For related access patterns, see DynamoDB query examples.
Similar Code Examples
- DynamoDB: Increment Counter
- DynamoDB: Query Global Secondary Index in Nodejs
- DynamoDB: Bulk Insert
- DynamoDB Get in Typescript
- DynamoDB: BatchGetItem
- DynamoDB: Not Begins With
- DynamoDB: Query Greater Than
- DynamoDB: Get Unique Values
- DynamoDB: Query JSON
- DynamoDB: Get Last Inserted Item
- DynamoDB: Create if Not Exists
- DynamoDB: Query KeyConditionExpression
- DynamoDB: Delete Multiple Items in Javascript
- DynamoDB: Get Last 10 Records
- DynamoDB: Attribute Not Null
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today