5

I am trying to scan for a value with column name S3KeyID and I'm looking for the value "newkey".
When I run this in test, it scans all 3 items in the dynamoDB Table and it finds 3 results and no matches.

import boto3
import json
import decimal
import calendar
import datetime
from boto3.dynamodb.conditions import Key, Attr

def lambda_handler(event, context):

    StartDateTime = datetime.datetime.now() - datetime.timedelta(minutes=10000)
    EndDateTime = datetime.datetime.now()


    # Helper class to convert a DynamoDB item to JSON.
    class DecimalEncoder(json.JSONEncoder):
        def default(self, o):
            if isinstance(o, decimal.Decimal):
                return str(o)
            return super(DecimalEncoder, self).default(o)

    dynamodb = boto3.resource('dynamodb')

    table = dynamodb.Table('Overwatch')

    print("Overwatch old messages")

    response = table.scan(
            #ExpressionAttributeNames = {'ST' : 'S3KeyID'},

            FilterExpression = "S3KeyID = :key",

            ExpressionAttributeValues =
            {   #":dateStart": {"S": StartDateTime},
                #":dateEnd": {"S": EndDateTime},
                ":key" : {"S" : "newkey"}
    #            ":S3KeyID : 1222433"
            }

            #ProjectionExpression="S3KeyID"
    )

    for i in response[u'Items']:
        print(json.dumps(i, cls=DecimalEncoder))
    return response

See result:

Items": [],
  "Count": 0,
  "ScannedCount": 3,

1 Answer 1

9

I guess that works better:

response = table.scan(
    FilterExpression = Attr('S3KeyID').eq('newkey')
)

Read the docs for more examples. Here is my inspiration:

Similarly you can scan the table based on attributes of the items. For example, this scans for all the users whose age is less than 27:

response = table.scan(
    FilterExpression=Attr('age').lt(27)
)
items = response['Items']
print(items)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Costin. that worked for me. I commented out all the ExpressionAttributeValues with it
Do you know if .lt works for a date saved as a string?
I do not know if .lt() works for strings, but give it a try and let us know. :) (I do everything in nodejs on aws)
Ah, I actually translated the datetime to epoch and then just treated it as an int and did it that way, works great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.