猿问

是否可以使用 javascript SDK 从亚马逊 Dynamodb 查询 JSON 数据

我需要有关 amazon Dynamo 的帮助。我正在寻找 dynamodb 中的特殊查询


我的 JSON 如下所示


{

  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'

}

我想查询 FARMERID 为“FAINKABR0001”的所有数据(农产品)。我浏览了所有示例,但似乎我只能对哈希键、排序键和使用 GSI 进行查询。我们可以使用AWS的Javascript SDK查询它吗?


互换的青春
浏览 135回答 2
2回答

ibeautiful

DynamoDB 中的操作Query根据主键值查找项目。GSI您可以查询具有复合主键(分区键和排序键)的任何表或二级索引 ( )。现在对于你的问题,你有两个选择:选项 1设为FARMERID您的 GSI选项2使用Scan方法并过滤结果现在您需要根据您的需求进行成本评估。每种方法都有其自身的优点和缺点。根据评论,一种方法可能是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");

人到中年有点甜

我还添加了我的整个代码,将来有人会遇到同样的问题,这里 FilterExpression 为 FilterExpression: "BusinessData.FARMERID = :farmeridvalue"在这里,我们需要给 FilterExpression 值提供我们想要查询的属性,即BusinessData.FARMERID并给出一个名称,正如我farmeridvalue现在给出的那样,您已将ExpressionAttributeValues我设置为搜索值,如下所示FAINKABR0001完整扫描码如下var params = {&nbsp; &nbsp; TableName: "Your_tableName",&nbsp; &nbsp; FilterExpression: "BusinessData.FARMERID = :farmeridvalue",&nbsp; &nbsp; ExpressionAttributeValues: {&nbsp; &nbsp; &nbsp; &nbsp; ":farmeridvalue" :"FAINKABR0001"&nbsp; &nbsp; }};docClient.scan(params, onScan);function onScan(err, data) {&nbsp; &nbsp; if (err) {&nbsp; &nbsp; &nbsp; &nbsp; console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // print all the movies&nbsp; &nbsp; &nbsp; &nbsp; console.log("Scan succeeded.", data);&nbsp; &nbsp; &nbsp; &nbsp; data.Items.forEach(function(Block) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log( "result",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Block.docType + ": ",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Block.timestamp, "- rating:",&nbsp; Block.BusinessData.FARMERID);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; // continue scanning if we have more movies, because&nbsp; &nbsp; &nbsp; &nbsp; // scan can retrieve a maximum of 1MB of data&nbsp; &nbsp; &nbsp; &nbsp; if (typeof data.LastEvaluatedKey != "undefined") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Scanning for more...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params.ExclusiveStartKey = data.LastEvaluatedKey;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; docClient.scan(params, onScan);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答