查询在控制台有效,但在代码中无效
Query works at the console but not in code
我的 DynamoDB table alexas
有这个带有键 "abc" 的项目,如下面的 DynamoDB 控制台所示:
但是,下面的查询returns没有结果:
const params = { TableName: "alexas",
KeyConditionExpression: "deviceId = :deviceId",
ExpressionAttributeValues: { ":deviceId": "abc"}
}
const docClient = new AWS.DynamoDB.DocumentClient();
docClient.query(params, (err, data) => {
if (err) {
console.error(err);
} else {
console.log(data);
}
});
上面的代码 returns null
for err
and in data
:
{ Items: [], Count: 0, ScannedCount: 0 }
我是 DynamoDB 表达式风格的新手。我从 here.
获取的代码有什么问题吗
如果我使用 scan
方法而不是 query
,并且 params
中只有 TableName
,我会在 table 中获取项目。这确认我正在对具有数据的正确 table 执行操作。
DynamoDB 的 Scan
操作不采用 KeyConditionExpression
- 只有 Query
操作采用此参数。 Scan 总是扫描整个 table,并且有一个 FilterExpression
到 post-filter 这些结果(但是请注意,您仍然需要为扫描整个 table 付费)。
例如Scan的官方文档:https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html
勾选QueryAPI
const params = { TableName: "alexas",
KeyConditionExpression: "deviceId = :deviceId",
ExpressionAttributeValues: {
":devideId":{
S: "abc", // here
}
}
}
const docClient = new AWS.DynamoDB.DocumentClient();
docClient.query(params, (err, data) => {
if (err) {
console.error(err);
} else {
console.log(data);
}
});
ExpressionAttributeValues
需要以不同的方式传递。
更新:
尝试使用 Exp 属性名称,(我不确定这是否会产生影响)
var params = {
TableName: "alexas",
KeyConditionExpression: "#d = :dId",
ExpressionAttributeNames:{
"#d": "email"
},
ExpressionAttributeValues: {
":dId": "abc"
}
};
查询没有返回任何数据,因为键值不匹配。
项目的 deviceId
是字符串 "abc"
而不是 abc
。请注意额外的引号。
该项目是使用 DynamoDB 控制台的创建编辑器插入的,如果该值已预期为字符串类型,则无需包含 ""
。
我的 DynamoDB table alexas
有这个带有键 "abc" 的项目,如下面的 DynamoDB 控制台所示:
但是,下面的查询returns没有结果:
const params = { TableName: "alexas",
KeyConditionExpression: "deviceId = :deviceId",
ExpressionAttributeValues: { ":deviceId": "abc"}
}
const docClient = new AWS.DynamoDB.DocumentClient();
docClient.query(params, (err, data) => {
if (err) {
console.error(err);
} else {
console.log(data);
}
});
上面的代码 returns null
for err
and in data
:
{ Items: [], Count: 0, ScannedCount: 0 }
我是 DynamoDB 表达式风格的新手。我从 here.
获取的代码有什么问题吗如果我使用 scan
方法而不是 query
,并且 params
中只有 TableName
,我会在 table 中获取项目。这确认我正在对具有数据的正确 table 执行操作。
DynamoDB 的 Scan
操作不采用 KeyConditionExpression
- 只有 Query
操作采用此参数。 Scan 总是扫描整个 table,并且有一个 FilterExpression
到 post-filter 这些结果(但是请注意,您仍然需要为扫描整个 table 付费)。
例如Scan的官方文档:https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html
勾选QueryAPI
const params = { TableName: "alexas",
KeyConditionExpression: "deviceId = :deviceId",
ExpressionAttributeValues: {
":devideId":{
S: "abc", // here
}
}
}
const docClient = new AWS.DynamoDB.DocumentClient();
docClient.query(params, (err, data) => {
if (err) {
console.error(err);
} else {
console.log(data);
}
});
ExpressionAttributeValues
需要以不同的方式传递。
更新: 尝试使用 Exp 属性名称,(我不确定这是否会产生影响)
var params = {
TableName: "alexas",
KeyConditionExpression: "#d = :dId",
ExpressionAttributeNames:{
"#d": "email"
},
ExpressionAttributeValues: {
":dId": "abc"
}
};
查询没有返回任何数据,因为键值不匹配。
项目的 deviceId
是字符串 "abc"
而不是 abc
。请注意额外的引号。
该项目是使用 DynamoDB 控制台的创建编辑器插入的,如果该值已预期为字符串类型,则无需包含 ""
。