javascript - How do I query an index properly with Dynamoose - Stack Overflow

I'm using Dynamoose to simplify my interactions with DynamoDB in a node.js application.I'm

I'm using Dynamoose to simplify my interactions with DynamoDB in a node.js application. I'm trying to write a query using Dynamoose's Model.query function that will search a table using an index, but it seems like Dynamoose is not including all of the info required to process the query and I'm not sure what I'm doing wrong.

Here's what the schema looks like:

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)

I'd like to be able to search for users by their email address, so I'm writing a query that looks like this:

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)
    })

I have a global secondary index defined for the email field:

When I try to execute this query, I'm getting the following error:

Error searching for users: ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.

Using the Dynamoose debugging output, I can see that the query winds up looking like this:

aws:dynamodb:query:request - {
"FilterExpression": "contains (#a0, :v0)",
"ExpressionAttributeNames": {
    "#a0": "email"
},
"ExpressionAttributeValues": {
    ":v0": {
        "S": "mel"
    }
},
"TableName": "user_qa",
"IndexName": "email-index"
}

I note that the actual query sent to DynamoDB does not contain KeyConditions or KeyConditionExpression, as the error message indicates. What am I doing wrong that prevents this query from being written correctly such that it executes the query against the global secondary index I've added for this table?

I'm using Dynamoose to simplify my interactions with DynamoDB in a node.js application. I'm trying to write a query using Dynamoose's Model.query function that will search a table using an index, but it seems like Dynamoose is not including all of the info required to process the query and I'm not sure what I'm doing wrong.

Here's what the schema looks like:

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)

I'd like to be able to search for users by their email address, so I'm writing a query that looks like this:

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)
    })

I have a global secondary index defined for the email field:

When I try to execute this query, I'm getting the following error:

Error searching for users: ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.

Using the Dynamoose debugging output, I can see that the query winds up looking like this:

aws:dynamodb:query:request - {
"FilterExpression": "contains (#a0, :v0)",
"ExpressionAttributeNames": {
    "#a0": "email"
},
"ExpressionAttributeValues": {
    ":v0": {
        "S": "mel"
    }
},
"TableName": "user_qa",
"IndexName": "email-index"
}

I note that the actual query sent to DynamoDB does not contain KeyConditions or KeyConditionExpression, as the error message indicates. What am I doing wrong that prevents this query from being written correctly such that it executes the query against the global secondary index I've added for this table?

Share Improve this question asked Jun 24, 2020 at 0:02 Mel StanleyMel Stanley 3742 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

As it turns out, calls like .contains(text) are used as filters, not query parameters. DynamoDB can't figure out if the text in the index contains the text I'm searching for without looking at every single record, which is a scan, not a query. So it doesn't make sense to try to use .contains(text) in this context, even though it's possible to call it in a chain like the one I constructed. What I ultimately needed to do to make this work is turn my call into a table scan with the .contains(text) filter:

Users.scan({ email: { contains: query.email }}).all().exec().then( ... )

I am not familiar with Dynamoose too much but the following code below will do an update on a record using node.JS and DynamoDB. See the key parameter I have below; by the error message you got it seems you are missing this.

To my knowledge, you must specify a key for an UPDATE request. You can checks the AWS DynamoDB docs to confirm.

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();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745223527a4617355.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信