I have an array of users id's and I want to get all users with that id from the dynamoDB table
Didn't find it in the documentation
any ideas?
I have an array of users id's and I want to get all users with that id from the dynamoDB table
Didn't find it in the documentation
any ideas?
I ended up using batchGet, an operation of AWS.DynamoDB.DocumentClient
There's no support for multiple items with same key, so I have to define the key over and over again like this:
var dynamoConfig = {
sessionToken: process.env.AWS_SESSION_TOKEN,
region: process.env.AWS_REGION
};
var dynamodbDocClient = new AWS.DynamoDB.DocumentClient(dynamoConfig);
var params = {
RequestItems: {
tableName: {
Keys: [
{
id: 'user1Id'
},
{
id: 'user2Id'
},
{
id: 'user3Id'
}
]
}
}
}
dynamodbDocClient.batchGet(paramsForQueringFormerEvaluators, function(err, data) {
if (err) {
console.log('createEvaluation: get former evaluators: err: ', err);
return;
}
var users = data.Responses.tableName;
console.log('createEvaluation: get former evaluators: ', users);
});
You can use the BatchGetItem API for this.
Of course, I can't help you with any code snippet without knowing your table schema. But you can look at the documentation here.
You can probably use Scan
method.
I'm currently using python boto3
package. Ref to scan
in boto3. So the code would be like:
table = boto3.resourse('dynamodb').Table('users-table-name')
response = table.scan(
ScanFilter={'user-id': {
'AttributeValueList': user_ids,
'ComparisonOperator': 'IN' }
}
)
But i'm not sure what is better, using BatchGetItem
or Scan
. Probably it depends on two factors:
table.item_count
)count(user_ids)
)If count(user_ids) << table.item_count
then you definitely need to use BatchGetItem
.
If count(user_ids) / table.item_count -> 1
then you need to use Scan
.
I wrote a function that converts a list of IDs into a list of documents using batchGet
:
async function getDocs(ids, docClient, tableName, key='id') {
const getParams = {
RequestItems: {
[tableName]: {
Keys: ids.map(id => ({[key]: id}))
}
}
}
return (await docClient.batchGet(getParams).promise()).Responses[tableName];
}