AWS Certified Developer - Associate Exam: Retrieve Attributes from DynamoDB Table

Querying DynamoDB Table for Specific Attributes

Prev Question Next Question

Question

You're currently working with a table in DynamoDB.

Your program has inserted several rows in the table.

You now need to read the data from the table and only retrieve certain attributes in the query.

Which of the following would you use for this purpose?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Answer - C.

The AWS Documentation mentions the following.

To read data from a table, you use operations such as GetItem, Query, or Scan.

DynamoDB returns all of the item attributes by default.

To get just some, rather than all of the attributes, use a projection expression.

A projection expression is a string that identifies the attributes you want.

To retrieve a single attribute, specify its name.

For multiple attributes, the names must be comma-separated.

Options A and B are incorrect since Projection Expression is available in both the scan and query operation.

Option D is invalid since you need to use the Projection Expression.

For more information on projection expressions, please refer to the below URL-

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ProjectionExpressions.html

The correct answer is C. Use the Projection Expression.

Explanation:

DynamoDB is a NoSQL database service provided by AWS that provides fast and predictable performance with seamless scalability. In DynamoDB, data is stored in tables, and each table can have one or more items (rows) and each item can have one or more attributes (columns). To retrieve data from a DynamoDB table, you can use two operations: Scan and Query.

  1. Scan operation: The scan operation reads all the items in a table and returns the result. This operation can be slow and expensive, especially for large tables, and it requires the use of filtering to narrow down the results.

  2. Query operation: The query operation retrieves items from a table that match a specific partition key value. It is faster and more efficient than the scan operation, especially for large tables.

When using the query operation, you can use a Projection Expression to retrieve only specific attributes from the table. A projection expression is a string that identifies the attributes to be retrieved from a table. The query operation supports the use of a Projection Expression to retrieve only specific attributes from the table.

Example:

Suppose you have a DynamoDB table named "Users" with the following items:

UserIdNameAgeGenderCity
1Alice28FemaleNew York
2Bob35MaleLondon
3Charlie42MaleSydney
4Dave19MaleNew York

If you want to retrieve only the names and ages of all the users from the table where the City is "New York," you can use the following query with a Projection Expression:

javascript
var params = { TableName: "Users", KeyConditionExpression: "City = :city", ExpressionAttributeValues: { ":city": "New York" }, ProjectionExpression: "Name, Age" }; docClient.query(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Items); } });

In this example, the Projection Expression "Name, Age" is used to retrieve only the Name and Age attributes of the items matching the query condition. The result will be:

yaml
Success [ { Name: 'Alice', Age: 28 }, { Name: 'Dave', Age: 19 } ]

Therefore, the correct answer is C. Use the Projection Expression.