1

I am working on a project with Dynamodb and Nodejs. I need a solution for query on multiple fields like select * from table where name=this and age=22 and active=this and area=this. I need a solution with query only scan is not allowed to me. If it is possible then please let me know with a sample nodejs script.

2
  • 1
    Please provide the key attributes for this table to provide the solution. Commented Mar 31, 2017 at 8:09
  • @notionquest you can use personId as key attribute. Commented Mar 31, 2017 at 8:45

1 Answer 1

5

Here is the code.

1) Change the table name

2) Change login credentials if you are using the AWS DynamoDB service. The below code uses local DynamoDB service

var AWS = require("aws-sdk");

var creds = new AWS.Credentials('akid', 'secret', 'session');

AWS.config.update({
    region : "us-west-2",
    endpoint : "http://localhost:8000",
    credentials : creds
});

var docClient = new AWS.DynamoDB.DocumentClient();

var table = "tablename";

var params = {
    TableName : table,
    KeyConditionExpression : 'personId = :personIdval', 
    FilterExpression : '#name= :nameVal and age= :ageVal and active=:activeVal and area=:areaVal',
     ExpressionAttributeNames : {
        '#name' : 'name'
    },
    ExpressionAttributeValues : {
        ':personIdval' : '7',
        ':nameVal' : 'this',
        ':ageVal' : 22,
        ':activeVal' : 'this',
        ':areaVal' : 'this'
    }
};

docClient.query(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err,
                null, 2));
    } else {
        console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.