Getting only one item or the first item of the table in Dynamo DB

Viewed 11388

Is there a way that I query only the first item appearing in the table in Dynamo DB? Like for instance if I have 20k records. I don't want to get all the 20k first, and then get the first. I want it to query only the first one found in the table without passing a primary key or a sort key.

2 Answers

Scan the table or index setting the Limit parameter to 1. If there is no filter expression this will return the first item.

From the DynamoDB documentation:

For example, suppose that you Scan a table with a Limit value of 6 and without a filter expression. The Scan result contains the first six items from the table.

Now suppose that you add a filter expression to the Scan. In this case, DynamoDB applies the filter expression to the six items that were returned, discarding those that do not match. The final Scan result contains six items or fewer, depending on the number of items that were filtered.

Query the table and get only the first result and only 1 object.

const params = {
        TableName: "Logins",
        Limit : 1
    },
    docClient = new AWS.DynamoDB.DocumentClient(),

    returnObj = {};
    docClient.scan(params, function(err, data){
        if(err) {
            console.log(err)
        } else {
          console.log(data)
        }
Related