gpt4 book ai didi

node.js - DynamoDB attribute_exists 不起作用

转载 作者:行者123 更新时间:2023-12-01 04:48:05 24 4
gpt4 key购买 nike

用这个敲我的头几个小时,我不明白为什么它不起作用🙁

我有一个 DynamoDBv2 表(顶级属性显示为单独的列),其中包含以下项目:

{
gateway: 'gateway1',
device_id: '2',
state: { temperature: 20, humidity: 30, pressure: 101049 },
timestamp: 1498331237261
},
{
gateway: 'gateway1',
device_id: '2',
state: { temperature: 20, humidity: 30, pressure: 101049, lat: 2, long: 1 },
timestamp: 1498331237262
}

主分区键: device_id (字符串)

主排序键: timestamp (数字)

我正在尝试获取具有以下属性的特定设备的最新项目 latlong .
const docClient = new AWS.DynamoDB.DocumentClient();
const id = 2;
const params = {
TableName: 'device_telemetry',
KeyConditionExpression: 'device_id = :id',
ProjectionExpression: '#s, #lat, #lng, #ts',
FilterExpression: 'attribute_exists(#lat) AND attribute_exists(#lng)',
ExpressionAttributeNames: {
'#s': 'state',
'#lat': 'state.lat',
'#lng': 'state.long',
'#ts': 'timestamp'
},
ExpressionAttributeValues: {
':id': id.toString()
},
ScanIndexForward: false,
Limit: 1
};

const onResult = (res) => {
console.info('devices res for: ', id, res);
};

console.info('query: ', params);
docClient.query(params).promise().then(onResult);

如果我使用 attribute_exists(#s) ,然后它有效,但对于 #lat#long它没有🙁

我究竟做错了什么? 😖

最佳答案

您不能直接访问嵌套属性。您应该以这种格式声明

 ExpressionAttributeNames: {
'#s': 'state',
'#lat': 'lat',
'#lng': 'long',
'#ts': 'timestamp'
},

并通过以下方式访问属性:
FilterExpression: 'attribute_exists(#s.#lat) AND attribute_exists(#s.#lng)',

关于node.js - DynamoDB attribute_exists 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44779615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com