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.

(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:

  1. The key condition query (i.e., the partition key hash) and optionally the sort key
  2. The filter expression (whatever query other attribute you want)

Load sample data

We give some examples below, but first we need some data:

  1. Install DynamoDB and run it locally, as we explained in How To Add Data in DynamoDB.
  2. Install node so we can run some JavaScript code.
  3. Install the Amazon SDK using npm, which is part of node:
npm install aws-sdk

Run these programs from the Amazon JavaScript examples:

  1. Create the Movies table by running MoviesCreateTable.js.
  2. Download the sample data from here and unzip it.
  3. Load some data by running MoviesLoadData.
Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article