How to Query Amazon Dynamodb
In This Article, We Explain the Basics of Dynamodb Queries. the Query Syntax Is Quite Different from Other Databases, So It Takes Some Time to Get Used to It...
In this article, we explain the basics of DynamoDB queries. The query syntax is quite different from other databases, so it takes some time to get used to it.
(This tutorial is part of our DynamoDB Guide. Use the right-hand menu to navigate.)
Hash key in DynamoDB
The primary reason for that complexity is that you cannot query DynamoDB without the hash key. So, it’s not allowed to query the entire database. That means you cannot do what you would call a full table scan in other databases.
However, the primary (partition) key can include a second attribute, which is called a sort key. The key query condition must be = (equals). But operators for the sort key can be:
- 〈
- 〉
- _
- 〈=
- 〉=
- begins_with
- between
Each query has two distinct parts:
- The key condition query (i.e., the partition key hash) and optionally the sort key
- The filter expression (whatever query other attribute you want)
Must Read
Load sample data
We give some examples below, but first we need some data:
- Install DynamoDB and run it locally, as we explained in How To Add Data in DynamoDB.
- Install node so we can run some JavaScript code.
- Install the Amazon SDK using npm, which is part of node:
npm install aws-sdk
Run these programs from the Amazon JavaScript examples:
- Create the Movies table by running MoviesCreateTable.js.
- Download the sample data from here and unzip it.
- Load some data by running MoviesLoadData.