OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Query dynamoDB with datetime filter expression - Incorrect operand

  • Thread starter Thread starter Matt Winer
  • Start date Start date
M

Matt Winer

Guest
I am try to query my dynamodb to get items older than 5min.

My item is stored as:

Code:
{
  "main_event_code": {
    "S": "1160"
  },
  "event_code": {
    "S": "9999"
  },
  "last_upload": {
    "S": "2024-05-10T04:09:29.614535Z"
  }
}

Python script:

Code:
    current_utc_time = datetime.now(timezone.utc)
    past_time = current_utc_time - timedelta(minutes=5)
    
    # Format both times as ISO 8601 strings
    current_utc_time_iso = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ')
    past_time_iso = past_time.strftime('%Y-%m-%dT%H:%M:%SZ')

    # Print the current UTC time and the time minus 5 minutes
    print(f"Current UTC Time: {current_utc_time_iso}")
    print(f"Time 5 Minutes Ago: {past_time_iso}")

    try:
        # Scan the table with a filter for items older than 'past_time'
        response = table.scan(
            FilterExpression="last_upload < :past_time",
            ExpressionAttributeValues={":past_time": {"S": past_time_iso}}
        )

I get:

Error querying DynamoDB table: An error occurred (ValidationException) when calling the Scan operation: Invalid FilterExpression: Incorrect operand type for operator or function; operator or function: <, operand type: M

I know the filterexpression works because using the AWS CLI I get the item returned

Code:
aws dynamodb scan --table-name ingestion_queue --filter-expression "last_upload < :val" --expression-attribute-values '{":val":{"S":"2024-05-10T04:23:08Z"}}'
<p>I am try to query my dynamodb to get items older than 5min.</p>
<p>My item is stored as:</p>
<pre><code>{
"main_event_code": {
"S": "1160"
},
"event_code": {
"S": "9999"
},
"last_upload": {
"S": "2024-05-10T04:09:29.614535Z"
}
}
</code></pre>
<p>Python script:</p>
<pre><code> current_utc_time = datetime.now(timezone.utc)
past_time = current_utc_time - timedelta(minutes=5)

# Format both times as ISO 8601 strings
current_utc_time_iso = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ')
past_time_iso = past_time.strftime('%Y-%m-%dT%H:%M:%SZ')

# Print the current UTC time and the time minus 5 minutes
print(f"Current UTC Time: {current_utc_time_iso}")
print(f"Time 5 Minutes Ago: {past_time_iso}")

try:
# Scan the table with a filter for items older than 'past_time'
response = table.scan(
FilterExpression="last_upload < :past_time",
ExpressionAttributeValues={":past_time": {"S": past_time_iso}}
)
</code></pre>
<p>I get:</p>
<blockquote>
<p>Error querying DynamoDB table: An error occurred (ValidationException) when calling the Scan operation: Invalid FilterExpression: Incorrect operand type for operator or function; operator or function: <, operand type: M</p>
</blockquote>
<p>I know the filterexpression works because using the AWS CLI I get the item returned</p>
<pre><code>aws dynamodb scan --table-name ingestion_queue --filter-expression "last_upload < :val" --expression-attribute-values '{":val":{"S":"2024-05-10T04:23:08Z"}}'
</code></pre>
 

Latest posts

A
Replies
0
Views
1
Ashrik Ahamed
A
A
Replies
0
Views
1
Ashrik Ahamed
A

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top