首页 > 解决方案 > Search in Dynamodb db

问题描述

I'm using Lambda, API-gateway & Dynamodb using python 3.6 I have a dynamodb table for order id:

OrderId is unique. Whenever I try to query by date I get the following error:

{
  "errorMessage": "An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema",
  "errorType": "ClientError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      8,
      "lambda_handler",
      "\"date\":day"
    ]

My code:

    import boto3
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('Shops')

    def lambda_handler(event, context):
        day = event['day']
        resp = table.get_item(Key={
        "date":day
        })
        return resp['Item']

I want to write a query that takes one date as input and will return all orders on that day:

This was my input:

{
  "day": "191215"
}

标签: pythonamazon-web-servicesaws-lambdaamazon-dynamodbaws-api-gateway

解决方案


您尝试做的事情在 DynamoDB 中是不可能的。无论您是查询还是尝试获取特定项目,您都必须始终提供主键。

当然,您可以扫描表,但您可能知道这是一项非常昂贵的操作。

您始终可以创建一个全局二级索引date因为它是主键并查询索引。


推荐阅读