简体   繁体   中英

DynamoDB : Getting attributes of every item of array

I'm trying to BatchGetItem with an array of primary keys, but the problem is BatchGetItem want

Keys : [ 
 { 'username' : 'username1'} 
 { 'username' : 'username2'}
]

instead of

Keys : [ 'username' : ':someUsernameArray' ]

Here is the part of my Lambda Function (in Node.js)

var follower_arr = Array.from(data.Item.followers.values);

var follower_params = {
    RequestItems : {
        USER : { 
            Keys :?,
            ProjectionExpression : "university.#name,full_name,username",
            "ConsistentRead": false,
            "ExpressionAttributeNames": { 
                "#name" : "name" 
             }    
        },
    }
}

docClient.batchGet(follower_params,function(err,data){
    if(err){
        callback(err,null);
    }else{
        callback(null,data);
    }
});

follower_arr value is an array of usernames such as,

['jack', 'michael_is_cool', 'mark_me_but', 'brownie_mr_brown']

So how can I get the attributes of all the elements of array which consists of primary keys?

You need to convert the array of usernames into an array of objects as show below. Use the resultant array in your query parameters.

follower_arr.forEach(function (ele) {
    keys.push({ username: ele })
}) 

var follower_params = {
    RequestItems : {
        USER : { 
            Keys :keys   
        },
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM