Victor Le
Victor Le

Reputation: 1788

Query data without key (AWS DynamoDB SDK Nodejs)

I just got started into using AWS Nodejs SDK For DynamoDb

I"m a little confused on how I can access a specific data without using the partition key.

Let's just assume I only have 1 item in my table with the Attribute "id" set up as my partition key

Table Name: Users

{
  "full_name": {
    "S": "John Cena"
  },
  "id": {
    "S": "ABC123"
  },
  "password": {
    "S": "youcantseeme"
  },
  "username": {
    "S": "AndHisNameIs"
  }
}

The code below would return the object above me if I query using my key

Btw I'm using dynamodb.query(params, function(err,data){}

Using Key to search

let params = {
    TableName : "Users",
    KeyConditionExpression: "#myid = :id",
    ExpressionAttributeNames:{
        "#myid": "id"
    },
    ExpressionAttributeValues: {
        ":id": {
            S: "ABC123"
        }
    }
};

Searching without the key

-I would like it if I can access that data without specifying the id string. For example If a user sends in a "username" and "password" I wouldn't have the ID so I wouldn't be able to look for it.

-The code below would return an error stating that I'm missing a partition key "id"

let params = {
    TableName : "Users",
    ExpressionAttributeNames:{
        "#myusername": "username"
    },
    KeyConditionExpression: "#myusername = :username",
    ExpressionAttributeValues: {
        ":username": {
            S: "AndHisNameIs"
        }
    }
};

My work around?

-replace the partition key with username instead of id? Is this how nosql databases work? I'm coming from a mysql environment so I was just able to just search a specific string or num or boolean by just asking for it in a certain column lol

Upvotes: 1

Views: 2890

Answers (1)

Mark B
Mark B

Reputation: 200562

Add a Global Secondary Index to the field you want to query on. Or do a full table Scan.

Upvotes: 2

Related Questions