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 = { 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); } }}