gpt4 book ai didi

javascript - 如何使用 dynamo db 和 node js 基于二级索引进行查询

转载 作者:行者123 更新时间:2023-11-30 15:22:15 25 4
gpt4 key购买 nike

如何使用 dynamo db 和 node js 使用二级索引进行查询

到目前为止,这是我的代码,但它不起作用

getObjectByAlternateKey: function (keyName, keyObj, indexName, callback, consistentRead) {
var params = {
TableName: this.tableName,
KeyConditionExpression: keyName + ' = :v_key',
ExpressionAttributeValues: converters.jsObjectToDynamoMap({ ':v_key': keyObj }),
IndexName: indexName,
ConsistentRead: !!consistentRead
};
this.dynamo.query(params, function (error, data) {
console.dir(data);
if (error) {
return callback(error);
}
if (data.Items && data.Items.length > 0) {
if (data.Items.length !== 1) {
console.warn("Got more than one item returned for query!", { query: params, data: data });
}
return callback(null, converters.dynamoMapToJsObject(data.Items[0]));
}

return callback(null, null);
});
},

这是我用来创建表格的 cloudformation 模板:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"TableName": {
"Description": "Table name to use",
"Type": "String",
"Default": "test-user-unique-ids-prod-ue1"
},
"ReadCapacityUnits": {
"Description": "Provisioned read throughput",
"Type": "Number",
"Default": "100",
"MinValue": "1",
"MaxValue": "10000",
"ConstraintDescription": "must be between 1 and 10000"
},
"WriteCapacityUnits": {
"Description": "Provisioned write throughput",
"Type": "Number",
"Default": "100",
"MinValue": "1",
"MaxValue": "10000",
"ConstraintDescription": "must be between 1 and 10000"
}
},
"Resources": {
"sparkUserUniqueIds": {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"TableName": {
"Ref": "TableName"
},
"AttributeDefinitions": [
{
"AttributeName": "guid",
"AttributeType": "S"
},
{
"AttributeName": "unique_id",
"AttributeType": "S"
}
],
"KeySchema": [
{
"AttributeName": "guid",
"KeyType": "HASH"
}
],
"GlobalSecondaryIndexes": [
{
"IndexName": "test-user-by-unique-id",
"KeySchema": [
{
"AttributeName": "unique_id",
"KeyType": "HASH"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": {
"Ref": "ReadCapacityUnits"
},
"WriteCapacityUnits": {
"Ref": "WriteCapacityUnits"
}
}
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": {
"Ref": "ReadCapacityUnits"
},
"WriteCapacityUnits": {
"Ref": "WriteCapacityUnits"
}
}
}
}
}
}

最佳答案

记录你的params值,应该是这样的

{ TableName: 'test-user-unique-ids-prod-ue1',
IndexName: 'test-user-by-unique-id',
KeyConditions:
{ unique_id:
{ ComparisonOperator: 'EQ',
AttributeValueList: [ { S: 'test' } ] } } }

关于javascript - 如何使用 dynamo db 和 node js 基于二级索引进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43555816/

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