Menu
Amazon DynamoDB
Getting Started Guide (API Version 2012-08-10)

Step 5: Query and Scan the Table

DynamoDB supports the Query operation on tables that have a composite primary key (partition key and sort key). You can also filter query results.

In addition, DynamoDB also supports the Scan operation on a table. This section provides introductory examples of using these operations.

Step 5.1: Run a Query

This section provides examples of Query operations. The queries are specified against the Music table. Remember, the table primary key is made of Artist (partition key) and SongTitle (sort key).

  • Query using only the partition key. For example, find songs by an artist.

  • Query using both the partition key and the sort key. For example, find songs by an artist and song title starting with a specific string.

  • Filter query results. Find songs by an artist and then return only those songs that have more than three radio stations playing them.

Query Using a Partition Key

Follow these steps to query for songs by an artist. Note that you use the KeyConditionExpression to specify the primary key. In this example it specifies only the partition key (Artist).

  1. Replace everything in the left side of the DynamoDB JavaScript shell window with the following code:

    
    var params = {
        TableName: "Music",
        KeyConditionExpression: "Artist = :artist",
        ExpressionAttributeValues: {
            ":artist": "No One You Know"
        }
    };
    
    docClient.query(params, function(err, data) {
        if (err)
            console.log(JSON.stringify(err, null, 2));
        else
            console.log(JSON.stringify(data, null, 2));
    });
    
    

    In the KeyConditionExpression, :artist is a token. The token value is provided in the ExpressionAttributeValues parameter. The : (colon) indicates that this is a placeholder for the value.

  2. Click the play button arrow to run the code. Only the songs by the band No One You Know are returned.

Query Using Key Attributes

Follow these steps to query for songs by an artist (The Acme Band) with song title starting with a specific string (S). Note that you use the KeyConditionExpression to specify the primary key. In this example it specifies both the partition key and sort key.

  1. Modify the params object from Query Using a Partition Key so that it looks like this:

    
    var params = {
        TableName: "Music",
        ProjectionExpression: "SongTitle",
        KeyConditionExpression: "Artist = :artist and begins_with(SongTitle, :letter)",
        ExpressionAttributeValues: {
            ":artist": "The Acme Band",
            ":letter": "S"
        }
    };
    

    Note the use of ProjectionExpression, which causes the query to return the SongTitle attribute only.

  2. Click the play button arrow to run the code. Only songs by The Acme Band, with titles that begin with the letter S, are returned. (There is only one song that meets this criteria in the Music table.)

Step 5.2: Filter Query Results

You can filter results of a query by adding the FilterExpression parameter. In this example you specify a query to find songs by an artist (The Acme Band). The query also specifies the FilterExpression to request DynamoDB to return only the song items that are being played on more than three radio stations.

  1. Modify the params object from Query Using a Partition Key so that it looks like this:

    
    var params = {
        TableName: "Music",
        ProjectionExpression: "SongTitle, PromotionInfo.Rotation",
        KeyConditionExpression: "Artist = :artist",
        FilterExpression: "size(PromotionInfo.RadioStationsPlaying) >= :howmany",
        ExpressionAttributeValues: {
            ":artist": "The Acme Band",
            ":howmany": 3
        },
    };
    
    

    Note the use of ProjectionExpression, which causes the query to return the top-level SongTitle attribute and the nested PromotionInfo.Rotation attribute.

    Also the FilterExpression specifies the size() function. For a list of predefined functions, go to the Expression Reference in the Amazon DynamoDB Developer Guide.

  2. Click the play button arrow to run the code. The response contains the only song by The Acme Band that is in heavy rotation on at least three radio stations.

Step 5.3: Scan the Table

You can use the Scan operation to retrieve all of the items in a table. In the following example you scan the Music table.

  1. Replace everything in the left side of the DynamoDB JavaScript shell window with the following code:

    
    var params = {
        TableName: "Music"
    };
    
    docClient.scan(params, function(err, data) {
        if (err)
            console.log(JSON.stringify(err, null, 2));
        else
            console.log(JSON.stringify(data, null, 2));
    });
    
    
  2. Click the play button arrow to run the code. All of the table items appear in the response.