DynamoDB: Not Begins With (Guide w/ Code Examples)

Provided by Rafal Wilinski
Last updated: October 23rd, 2026
NOT begins_with is valid in a filter or condition, but not as a sort-key condition in KeyConditionExpression. This example excludes non-key category values beginning with archived.
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 lastKey; do { const page = await ddb.send(new QueryCommand({ TableName: "Orders", KeyConditionExpression: "#pk = :pk", FilterExpression: "NOT begins_with(#category, :prefix)", ExpressionAttributeNames: { "#pk": "pk", "#category": "category" }, ExpressionAttributeValues: { ":pk": "CUSTOMER#123", ":prefix": "archived" }, ExclusiveStartKey: lastKey, })); for (const item of page.Items ?? []) console.log(item); lastKey = page.LastEvaluatedKey; } while (lastKey && Object.keys(lastKey).length > 0);
A Query filter cannot reference that query target's partition key or sort key. To exclude sort-key prefixes, use supported key ranges or filter returned items in your application.
Reference: AWS Query API. For related access patterns, see DynamoDB query examples.
Similar Code Examples
- DynamoDB Get in Typescript
- DynamoDB: Query Date Range
- DynamoDB: Query Items
- DynamoDB BatchWriteItem in Typescript
- DynamoDB: Attribute Not Null
- DynamoDB: Like
- DynamoDB: Query JSON
- DynamoDB: Query Group By
- DynamoDB: Query Global Secondary Index in Nodejs
- DynamoDB: Delete Multiple Items in Javascript
- DynamoDB: Delete All Items With Partition Key
- DynamoDB: Get Multiple Items
- DynamoDB: BatchGetItem
- DynamoDB: Get Random Item
- DynamoDB: Get All Items
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today
Product Features
DynamoDB Tools
- SSH GUI Client
- Chat With Cloud
- MongoDB GUI Client
- AWS Cost Monitoring
- DynamoDB Query Builder
- DynamoDB Code Examples
- DynamoDB Node.js Queries
- DynamoDB JSON Converter
- DynamoDB UUID Generator
- DynamoDB Local Admin GUI
- DynamoDB Pricing Calculator
- DynamoDB Single-Table Design
- Best DynamoDB GUI Clients
- Dynobase vs DynoTable