简体   繁体   English

dynamodb查询ExpressionAttributeValues javascript

[英]dynamodb query ExpressionAttributeValues javascript

Hey guys so I'm trying to query my dynamo db GIS index for any string is more than a year old on the checkdate column, so my questions is how do you construct the KeyConditionExpression or ExpressionAttributeValues? 嗨,大家好,我想在dynamo db GIS索引中查询checkdate列上是否有超过一年的字符串,所以我的问题是如何构造KeyConditionExpression或ExpressionAttributeValues? Any help is appreciated! 任何帮助表示赞赏! Refer to code below: 请参考下面的代码:

 console.log("Loading function"); var AWS = require('aws-sdk'); var lastyeartoday = new Date(); var dd = lastyeartoday.getDate(); var mm = lastyeartoday.getMonth()+1; var yyyy = lastyeartoday.getFullYear()-1; var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var s3 = new AWS.S3(); function date(err, lastyeartoday) { if(dd<10) { dd = '0'+dd; } if(mm<10) { mm = '0'+mm; } } lastyeartoday = mm + '/' + dd + '/' + yyyy; console.log(lastyeartoday); exports.handler = function(data, context, callback) { var params = { TableName: "S3_log", IndexName: "Checkdate-index", "Select": "ALL_ATTRIBUTES", KeyConditionExpression: 'Checkdate = :checkdate', ExpressionAttributeValues: { ":checkdate": {S: lastyeartoday.toString()}, }, ScanIndexForward: true, Limit: 1, ConsistentRead: false, ReturnConsumedCapacity: 'NONE', }; ddb.query(params, function(err, data) { if (err) { callback(err, null); } else { return(null, data); }; 

The Query API doesn't allow greater than operator for hash key attribute. Query API不允许使用大于运算符的哈希键属性。

Two approaches:- 两种方法:

1) Redesign the GSI to have Checkdate as sort key rather than hash key. 1)重新设计GSI,以将Checkdate作为排序键而不是哈希键。 In that case, you may need to define another attribute as hash key. 在这种情况下,您可能需要定义另一个属性作为哈希键。 Also, you need to include the hash key in KeyConditionExpression . 另外,您需要在KeyConditionExpression包含哈希键。 I am not sure whether this would satisfy your use case scenario. 我不确定这是否可以满足您的用例场景。 However, if you wanted to avoid full table scan and use Query API, then you need hash key value. 但是,如果要避免全表扫描并使用查询API,则需要哈希键值。 Without hash key value, you can't use Query API. 没有哈希键值,就无法使用查询API。

KeyConditionExpression: 'somenewhashkey = :hashkey AND Checkdate > :checkdate',

2) Use Scan API with FilterExpression 2)将Scan API与FilterExpression一起FilterExpression

FilterExpression: "Checkdate > :checkdate",

Please note that this would perform full table or index scan which is a costly operation 请注意,这将执行全表扫描或索引扫描,这是一项昂贵的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM