简体   繁体   English

如何在Dynamo DB中使用条件表达式编写查询

[英]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. 我想查询发电机数据库表以获取公司ID,但是同一表需要哈希键,因此我的查询是这样的。

        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. 我的查询仅在查询也具有公司ID的值时才有效,但是我想获取公司ID如何实现此目的。 In my node js 在我的节点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 . 您应该执行query ,而不是getItem ,该query只允许您指定userId

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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