1

I need help on amazon Dynamo. I am looking to special query in dynamodb

my JSON looks below

{
  blocknumber: '20',
  BusinessData: {
    BASE_UNIT: 'kg',
    FARMERID: 'FAINKABR0001',
    FARMLOCATION: 'Farm 3927',
    GAPINFO: {},
    PLANTINGDATE: '2020-11-02T18:30:00.000Z',
    PRODUCEQUANTITES: [
      {
        name: 'Priya',
        qty: 200
      }
    ],
    SELECTED_UNIT: {
      NAME: 'kg'
    }
  },
  chaincodeID: 'producechaincode',
  docType: 'Produce',
  PRID: 'PRFAINKABR0007',
  PRODUCE: 'Sweetcorn',
  STATUS: 'Approved',
  timestamp: '2020-12-06T13:03:08.857Z'
}

I would like to query all Data(Produce) where FARMERID is 'FAINKABR0001'. I went through all of the examples but it seems that I can query only on hash key, sort key and using GSI. Can we query it using Javascript SDK of AWS?

Thanks in advance

2 Answers 2

2

The Query operation in DynamoDB finds items based on primary key values. You can query any table or secondary index (GSI) that has a composite primary key (a partition key and a sort key).

Now for your question, you have two options:
Option 1

  • Make FARMERID as your GSI

Option 2

  • Use Scan method and filter the result

Now you will need to do cost evaluation based on your need. Each method has it's own pros and cons.

PFB some references:

Scan-JS SDK

Query-DDB

Based on comment, one approach could be

var data = 
[
    {
  blocknumber: '20',
  BusinessData: {
    BASE_UNIT: 'kg',
    FARMERID: 'FAINKABR0001',
    FARMLOCATION: 'Farm 3927',
    GAPINFO: {},
    PLANTINGDATE: '2020-11-02T18:30:00.000Z',
    PRODUCEQUANTITES: [
      {
        name: 'Priya',
        qty: 200
      }
    ],
    SELECTED_UNIT: {
      NAME: 'kg'
    }
  },
  chaincodeID: 'producechaincode',
  docType: 'Produce',
  PRID: 'PRFAINKABR0007',
  PRODUCE: 'Sweetcorn',
  STATUS: 'Approved',
  timestamp: '2020-12-06T13:03:08.857Z'
},
{
  blocknumber: '20',
  BusinessData: {
    BASE_UNIT: 'kg',
    FARMERID: 'FAINKABR0002',
    FARMLOCATION: 'Farm 3927',
    GAPINFO: {},
    PLANTINGDATE: '2020-11-02T18:30:00.000Z',
    PRODUCEQUANTITES: [
      {
        name: 'Priya',
        qty: 200
      }
    ],
    SELECTED_UNIT: {
      NAME: 'kg'
    }
  },
  chaincodeID: 'producechaincode',
  docType: 'Produce',
  PRID: 'PRFAINKABR0007',
  PRODUCE: 'Sweetcorn',
  STATUS: 'Approved',
  timestamp: '2020-12-06T13:03:08.857Z'
},

{
  blocknumber: '20',
  BusinessData: {
    BASE_UNIT: 'kg',
    FARMERID: 'FAINKABR0001',
    FARMLOCATION: 'Farm 3927',
    GAPINFO: {},
    PLANTINGDATE: '2020-11-02T18:30:00.000Z',
    PRODUCEQUANTITES: [
      {
        name: 'Priya',
        qty: 200
      }
    ],
    SELECTED_UNIT: {
      NAME: 'kg'
    }
  },
  chaincodeID: 'producechaincode',
  docType: 'Produce',
  PRID: 'PRFAINKABR0007',
  PRODUCE: 'Sweetcorn',
  STATUS: 'Approved',
  timestamp: '2020-12-06T13:03:08.857Z'
}
];

function filterResponse(data, id) {
  for(var i = 0; i < data.length; i++) {
    if(data[i].BusinessData.FARMERID === id ) {
      console.log(data[i]);
    }
  }
}

filterResponse(data, "FAINKABR0001");

Sign up to request clarification or add additional context in comments.

8 Comments

Mark the answer accepted if it solves your problem. Thanks 🙌
Thanks Atul for your answer I mentioned that we can do using GSI but we can use only 20 GSI per table so I would like to go option 2 but when I checked link you shared for option 2 it also not satisfied my question as its showing FilterExpression only primary key as 'year' but I am looking something like into json structure as mention in question.
FilterExpression specifies a condition that returns only items that satisfy the condition. All other items are discarded.
FilterExpression need not be PK
DDB Scan basically gets all the data stored in dynamoDB, and it is upto you how you want to use it. You can even write your own filter method if you like
|
0

Thanks @Atul Kumar for Help I have also added my whole code my be in future somebody face same issue Here FilterExpression as FilterExpression: "BusinessData.FARMERID = :farmeridvalue"

Here we need to give FilterExpression value which attribute we want to query i.e BusinessData.FARMERID and give one name as I give farmeridvalue now you have set ExpressionAttributeValues as search value for me as FAINKABR0001

see whole scan code as below

var params = {
    TableName: "Your_tableName",
    FilterExpression: "BusinessData.FARMERID = :farmeridvalue",
    ExpressionAttributeValues: {
        ":farmeridvalue" :"FAINKABR0001"

    }
};

docClient.scan(params, onScan);

function onScan(err, data) {
    if (err) {
        console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        // print all the movies
        console.log("Scan succeeded.", data);
        data.Items.forEach(function(Block) {
           console.log( "result",
                Block.docType + ": ",
                Block.timestamp, "- rating:",  Block.BusinessData.FARMERID);
        });

        // continue scanning if we have more movies, because
        // scan can retrieve a maximum of 1MB of data
        if (typeof data.LastEvaluatedKey != "undefined") {
            console.log("Scanning for more...");
            params.ExclusiveStartKey = data.LastEvaluatedKey;
            docClient.scan(params, onScan);
        }
    }
}

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.