我使用动力糖来简化与DynamoDB在node.js应用程序中的交互。我试图使用Dynamoose的Model.query函数编写一个查询,该函数将使用索引搜索表,但似乎Dynamoose没有包含处理查询所需的所有信息,我不知道我做错了什么。
下面是模式的样子:
const UserSchema = new dynamoose.Schema({
"user_id": {
"hashKey": true,
"type": String
},
"email": {
"type": String,
"index": {
"global": true,
"name": "email-index"
}
},
"first_name": {
"type": String,
"index": {
"global": true,
"name": "first_name-index"
}
},
"last_name": {
"type": String,
"index": {
"global": true,
"name": "last_name-index"
}
}
)
module.exports = dynamoose.model(config.usersTable, UserSchema)我希望能够通过用户的电子邮件地址搜索用户,因此我正在编写如下查询:
Users.query("email").contains(query.email)
.using("email-index")
.all()
.exec()
.then( results => {
res.status(200).json(results)
}).catch( err => {
res.status(500).send("Error searching for users: " + err)
})我为电子邮件字段定义了一个全局辅助索引:

当我尝试执行这个查询时,我会得到以下错误:
Error searching for users: ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.使用Dynamoose调试输出,我可以看到查询的结果如下所示:
aws:dynamodb:query:request - {
"FilterExpression": "contains (#a0, :v0)",
"ExpressionAttributeNames": {
"#a0": "email"
},
"ExpressionAttributeValues": {
":v0": {
"S": "mel"
}
},
"TableName": "user_qa",
"IndexName": "email-index"
}我注意到发送给DynamoDB的实际查询不包含KeyConditions或KeyConditionExpression,正如错误消息所指示的那样。如果不能正确编写该查询,从而使其针对我为该表添加的全局辅助索引执行查询,那么我做了什么错事呢?
发布于 2020-06-24 20:45:59
事实证明,像.contains(text)这样的调用被用作过滤器,而不是查询参数。DynamoDB无法找出索引中的文本是否包含我正在搜索的文本,而不查看每条记录,这是一个扫描,而不是一个查询。因此,尝试在这种上下文中使用.contains(text)是没有意义的,即使在我构建的链中调用它也是可能的。要完成这项工作,我最终需要做的是使用.contains(text)过滤器将调用转换为表扫描:
Users.scan({ email: { contains: query.email }}).all().exec().then( ... )发布于 2020-06-24 11:19:15
我不太熟悉Dynamoose,但是下面的代码将使用node.JS和DynamoDB对记录进行更新。请参阅下面的关键参数;根据您得到的错误消息,您似乎遗漏了这个参数。
据我所知,您必须为更新请求指定一个键。您可以检查AWS DynamoDB文档以确认。
var params = {
TableName: table,
Key: {
"id": customerID,
},
UpdateExpression: "set customer_name= :s, customer_address= :p, customer_phone= :u, end_date = :u",
ExpressionAttributeValues: {
":s": customer_name,
":p": customer_address,
":u": customer_phone
},
ReturnValues: "UPDATED_NEW"
};
await docClient.update(params).promise();https://stackoverflow.com/questions/62545576
复制相似问题