简体   繁体   中英

How to write a query in dynamo db with a conditioned expression

I want to query a dynamo db table for getting company id but the same table need a hash key so my query is something like this.

        var optsq = {
          'ConsistentRead': true,
          'AttributesToGet': ['companyid'],
          TableName : usertable,
          Key : {
            "userid" : {
              "S" : usrname
            },
            "comapnyid" :{
              "S":''
            }
          }
        };

My query will only work if the query has the value of company id as well but i want to get company id how can i achive this. In my node js

dynamodb.getItem(optsq, function(err, compdata) {
  if(err){
    console.log(err);
  }else{
    console.log(compdata);
  }

Instead of getItem , you should do a query , which allows you to specify only the userId .

var optsq  = {
    'ConsistentRead': true,
    TableName : usertable,
    KeyConditionExpression: "userid = :userid",
    ExpressionAttributeValues: { ":userid": usrname },
    ProjectionExpression: "companyid" }

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