Do multiple DynamoDb queries using Lambda function


I want to write some logic like fetch post with time limit 1 day(created date) and if the list of items are smaller then 20 then fetch the post within 2 days(created date).


const params = {
  TableName: 'Todo-xxxxxxxxxxxxxxx-dev',
  IndexName: 'title-createdAt-index',
  "Limit": 10,
  KeyConditionExpression: '#title =:value and #createdAt BETWEEN :start AND :end',
  ExpressionAttributeValues: { ':value': 'hi',
                                  ':end':  '2023-06-30T03:47:35.343Z'
  ExpressionAttributeNames: { '#title': 'title',

if the above code returns 10 list items then I will fetch with time limit ':start':'2023-06-28T03:47:35.343Z', ':end': '2023-06-29T03:47:35.343Z' and if it returns 6 items then I will fetch items again 1 days ago. Is this a good practice?

1 Answer

According to your question,

You have a DynamoDB Table which has a partition key userId and sort key createdAt. These attributes will be combined as Primary Key. I created an example table posts with the following items Enter image description here

You are saying that you want to fetch posts within 1 day and if they are less than 20, you need to fetch posts within 2 days. This is not a good practice. I think what you want is to fetch 20 latest posts of a user.

This is the lambda code that fetches last 20 items(posts) of a user.

Note:- runtime: nodejs 18x, Used AWS SDK for JavaScript v3

import { DynamoDBClient, QueryCommand, ScanCommand } from "@aws-sdk/client-dynamodb";
export const ddbClient = new DynamoDBClient();

export const handler = async(event) => {
    const endDate = '2023-01-07T17:18:44.219Z';
    const params = {
        TableName: 'posts',
        KeyConditionExpression: 'userId = :userId AND createdAt <= :endDate',
        ExpressionAttributeValues: {
          ':endDate': {S: endDate},
          ':userId': {S: '1'}
        ScanIndexForward: false,
    const result = await ddbClient.send(new QueryCommand(params));
    const response = {
    statusCode: 200,
    body: JSON.stringify(result.Items),
    return response;

Here, I have hardcoded the userId and endDate. You can pass it through event object. Limit: 20 means it give only 20 Items and ScanIndexForward: false gives the items in descending order

Hope this answer helps

profile picture
answered 2 years ago
  • If this returns post less than 20 then what to do? your end date is const endDate = '2023-01-07T17:18:44.219Z'; , i need to fetch again 1 day minus form the date you added

  • The createdAt <= :endDate means that you want everything less than that specific date. It starts with the most recent date from that date and goes to least recent due the ScanIndexForward: false. You also can omit the createdAt <= :endDate if the :endDate is the time of the query.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions