-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add note regarding bitwise operator usage on DynamoDB conditions page #4117
Comments
Thanks for raising this issue — I brought this up for discussion with the team and it was noted that the AND/OR/NOT are intentionally excluded from that documentation and the recommendation is to use Python bitwise operators instead. |
This issue is now closed. Comments on closed issues are hard for our team to see. |
@tim-finnigan I don't understand how you use Python bitwise operators to achieve the same thing as AND/OR/NOT, and I failed to find anything about this in the documentation. Can you please provide an example, or preferably a documentation link? Thanks. |
@SamStephens to use bitwise operators you can try the following: import boto3
from boto3.dynamodb.conditions import Attr
# Create a DynamoDB resource
dynamodb = boto3.resource('dynamodb')
# Specify the table name
table_name = 'test_table'
table = dynamodb.Table(table_name)
# Update an item's string attribute based on a condition expression
item_id = 'foo-key'
update_expression = 'SET item_name = :new_name'
condition_expression = Attr('item_status').exists() & Attr('item_status').eq('active')
expression_attribute_values = {
':new_name': 'Updated item'
}
# Execute the update operation with the condition expression
table.update_item(
Key={'id': item_id},
UpdateExpression=update_expression,
ConditionExpression=condition_expression,
ExpressionAttributeValues=expression_attribute_values
) Or you can use standard comparison operators as documented in the DynamoDB user guide. For example: import boto3
# Create a DynamoDB resource
dynamodb = boto3.resource('dynamodb')
# Specify the table name
table_name = 'test_table'
table = dynamodb.Table(table_name)
# Update an item's string attribute based on a condition expression
item_id = 'foo-key'
update_expression = 'SET item_name = :new_name'
condition_expression = 'attribute_exists(item_status) AND item_status = :active_status'
expression_attribute_values = {
':new_name': 'Updated item',
':active_status': 'active'
}
# Execute the update operation with the condition expression
table.update_item(
Key={'id': item_id},
UpdateExpression=update_expression,
ConditionExpression=condition_expression,
ExpressionAttributeValues=expression_attribute_values
) |
@tim-finnigan I don't see any documentation of either the ability to use bitwise operators with Why are AND/OR/NOT are intentionally excluded from that documentation? Is it because they're consider too complex? Is it because As far as using a bare string for condition_expression, it's documented in the API documentation you link to, but not the boto3 documentation. Reading that documentation, I thought you had to use the classes provided in |
@SamStephens I don't think you saw the update to my previous comment - that now includes an example using a bitwise operator. Even though AND/OR/NOT was originally included in the conditions file, those aren't documented for Boto3 because the team recommends using the existing Python operators. |
@tim-finnigan I saw your comment. What I'm saying is the fact that bitwise operators can be applied to the classes in |
I see, thanks for clarifying — it looks like an example of bitwise operators can be found here: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html#querying-and-scanning
But for better visibility I think a note describing that could also be added somewhere here to show on that documentation page. I'll reopen this and update the title. |
Describe the issue
I raised #4112 because I thought DynamoDB attribute expression AND/OR/NOT conditions were not available via boto3.
However they are available as shown in
boto3/boto3/dynamodb/conditions.py
Lines 158 to 238 in 096e458
However https://boto3.amazonaws.com/v1/documentation/api/latest/reference/customizations/dynamodb.html#dynamodb-conditions does not include documentation of these conditions.
Links
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/customizations/dynamodb.html#dynamodb-conditions
The text was updated successfully, but these errors were encountered: