簡體   English   中英

如何使用Dynamo DB和Node JS基於二級索引進行查詢

[英]How do I query based on secondary indexes using dynamo db and node js

如何使用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' } ] } } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM