6.3: Projection expressions

A projection expression is used to specify exactly the attributes you want to receive from the DynamoDB server. A projection expression is similar to a filter expression in that its main utility is in reducing the amount of data sent over the wire in your response.

Let us write a Query command that returns all the customers but only their firstname, lastname and phone. We do not need email and zipcode here.


          import { QueryCommand } from "@aws-sdk/lib-dynamodb";
          import ddbDocClient from "@/lib/clients/dynamoDBClient";
          import { dbName } from "@/lib/constants";

          const dbCommand = new QueryCommand({
            TableName: dbName,
            KeyConditionExpression: "#pk_key = :pk_value",
            ProjectionExpression:
                "#pk_key, #sk_key, #firstName_key, #lastName_key, #phone_key",
            ExpressionAttributeNames: {
                "#pk_key": "pk",
                "#sk_key": "sk",
                "#firstName_key": "firstName",
                "#lastName_key": "lastName",
                "#phone_key": "phone",
            },
            ExpressionAttributeValues: {
                ":pk_value": "CUSTOMERS",
            },
            });

            const dbResponse = await ddbDocClient.send(dbCommand);
        
Primary KeyAttributes
Partition key: pkSort key: skfirstNamelastNamephone

The projection expression can also be used to access nested properties, such as in a list or map attribute. If you have a large object and you know the exact property you want, you can really cut down on the bandwidth by specifying only the elements that you need.

Projection expressions are subject to the same caveats as filter expressions—they are evaluated after the items are read from the table and the 1MB limit is reached.