DynamoDB: Get Random Item (Guide w/ Code Examples)
Provided by Rafal Wilinski
To retrieve a random item from a DynamoDB table using the JavaScript SDK, you can use the query
method and specify the ScanIndexForward
parameter as false
. This will return the items in random order. To retrieve only one item, you can also specify a Limit
parameter with a value of 1.
const AWS = require('aws-sdk'); const dynamoDb = new AWS.DynamoDB.DocumentClient(); const params = { TableName: 'myTable', Limit: 1, ScanIndexForward: false }; dynamoDb.query(params, function(err, data) { if (err) { console.log(err); } else { console.log(data.Items[0]); } });
Note that ScanIndexForward
parameter only works with query
method. In case you need to use scan
method, you need need to fetch all the items and select one randomly.
Similar Code Examples
- DynamoDB: Like
- DynamoDB: Bulk Insert
- DynamoDB: Get Last 10 Records
- DynamoDB: Not Between
- DynamoDB: Delete All Items With Partition Key
- DynamoDB: Attribute Not Null
- DynamoDB: Query Items
- DynamoDB: Get List Of Items
- DynamoDB: Delete Table
- DynamoDB: Not Begins With
- DynamoDB: Batch Get
- DynamoDB: Create if Not Exists
- DynamoDB: Query Global Secondary Index in Nodejs
- DynamoDB: Delete
- DynamoDB: Get Multiple Items
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase