Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 989
JLT Avatar asked Sep 05 '18 12:09

JLT


People also ask

How can you get a single item in DynamoDB?

Getting an Item Be sure to configure the SDK as previously shown. To access DynamoDB, create an AWS. DynamoDB service object. To identify the item to get, you must provide the value of the primary key for that item in the table.

What DynamoDB data type can store only one value?

DynamoDB supports many different data types for attributes within a table. They can be categorized as follows: Scalar Types – A scalar type can represent exactly one value. The scalar types are number, string, binary, Boolean, and null.

Does DynamoDB Query return all items?

The Query operation in Amazon DynamoDB finds items based on primary key values. You must provide the name of the partition key attribute and a single value for that attribute. Query returns all items with that partition key value.

Which of the following is the fastest way to get an item from DynamoDB?

GetItem – Retrieves a single item from a table. This is the most efficient way to read a single item because it provides direct access to the physical location of the item. (DynamoDB also provides the BatchGetItem operation, allowing you to perform up to 100 GetItem calls in a single operation.)


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.

like image 145
cementblocks Avatar answered Nov 14 '22 21:11

cementblocks


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)
        }
like image 36
Nayan Patel Avatar answered Nov 14 '22 21:11

Nayan Patel