Amazon DynamoDB Tutorial 0/30 lessons ~6 min read Lesson 18

    Pagination, Filter Expressions, and Projections

    Real list screens need pages, filters, and only the attributes they display.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Real list screens need pages, filters, and only the attributes they display. DynamoDB pagination is cursor-based: each page returns a LastEvaluatedKey that you pass back as ExclusiveStartKey. Filters and projections shape what is returned, but neither reduces what is read.

    Purpose of this lesson

    You will implement cursor pagination for an API, understand how Limit interacts with filters, and trim responses with projection expressions.

    Understanding the topic

    Rules to remember:

    • Limit caps the number of items evaluated, before the filter — a page can contain fewer items than the limit, or none, while LastEvaluatedKey is still set.
    • Pages stop at 1 MB of data read even if Limit is not reached.
    • ProjectionExpression returns selected attributes; it does not lower consumed capacity.
    • There is no offset-based paging; to jump to page N you must read through earlier pages or design keys for direct access.
    • Select: "COUNT" returns only counts, but still reads (and charges for) the matching items.

    Informative example

    API-friendly pagination with an opaque cursor:

    ts
    import { QueryCommand } from "@aws-sdk/lib-dynamodb";
    import { ddb, TABLE } from "./ddb";
    const encode = (key?: Record<string, unknown>) =>
    key ? Buffer.from(JSON.stringify(key)).toString("base64url") : undefined;
    const decode = (cursor?: string) =>
    cursor ? JSON.parse(Buffer.from(cursor, "base64url").toString()) : undefined;
    export async function listOrders(customerId: string, cursor?: string) {
    const page = await ddb.send(new QueryCommand({
    TableName: TABLE,
    KeyConditionExpression: "PK = :pk AND begins_with(SK, :o)",
    ExpressionAttributeValues: { ":pk": `CUSTOMER#${customerId}`, ":o": "ORDER#" },
    ProjectionExpression: "SK, #total, #status",
    ExpressionAttributeNames: { "#total": "total", "#status": "status" },
    ScanIndexForward: false,
    Limit: 20,
    ExclusiveStartKey: decode(cursor),
    }));
    return { items: page.Items ?? [], nextCursor: encode(page.LastEvaluatedKey) };
    }

    Validate decoded cursors on the server (for example check that PK matches the requested customer) so clients cannot use a cursor to read another customer's partition.

    Useful templates

    Fill a page when using a filter

    If you often filter on the same attribute, put it in the key (or a sparse GSI) instead — filters still pay for every item read.

    ts
    export async function listShipped(customerId: string, pageSize = 20) {
    const items: Record<string, unknown>[] = [];
    let ExclusiveStartKey: Record<string, unknown> | undefined;
    do {
    const page = await ddb.send(new QueryCommand({
    TableName: TABLE,
    KeyConditionExpression: "PK = :pk AND begins_with(SK, :o)",
    FilterExpression: "#status = :shipped",
    ExpressionAttributeNames: { "#status": "status" },
    ExpressionAttributeValues: { ":pk": `CUSTOMER#${customerId}`, ":o": "ORDER#", ":shipped": "SHIPPED" },
    ExclusiveStartKey,
    }));
    items.push(...(page.Items ?? []));
    ExclusiveStartKey = page.LastEvaluatedKey;
    } while (ExclusiveStartKey && items.length < pageSize);
    return items.slice(0, pageSize);
    }

    Best practices

    • Return opaque cursors, never raw keys, from public APIs.
    • Move frequent filters into sort keys or sparse indexes.
    • Use projection expressions to keep responses small.

    Common mistakes

    • Treating an empty page as the end of results while LastEvaluatedKey is still set.
    • Expecting Limit to mean 'items returned'.
    • Building total counts by scanning on every request — maintain counters instead.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    2 questions
    1IntermediateQuestionWhy might a Query with Limit 10 return 3 items?+

    Answer

    Limit applies to items evaluated before the FilterExpression. If only 3 of the 10 evaluated items pass the filter, 3 are returned, with a LastEvaluatedKey to continue.
    2AdvancedQuestionHow do you implement 'jump to page 50' in DynamoDB?+

    Answer

    There is no offset. Either page through with cursors and cache them, or redesign so the target is addressable by key (for example bucketed sort keys by date). Most UIs use 'load more' with cursors instead.

    Hands-on exercise

    Hands-on: insert 45 orders for one customer and page through them with listOrders. Assert you get pages of 20, 20, and 5 items, and that the final nextCursor is undefined.

    Summary

    Pagination is cursor-based through LastEvaluatedKey. Limit counts evaluated items, filters and projections shape output but not cost, and frequent filters belong in keys or sparse indexes.

    Ready to mark this lesson complete?Track your journey across the entire course.