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.
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.
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.
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.
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.)
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With