1

I have a table that contains an item with the following attributes:

{
  "country": "USA",
  "names": [
    "josh",
    "freddy"
  ],
  "phoneNumber": "123",
  "userID": 0
}

I'm trying to query an item in a DynameDB by looking for a name using python. So I would write in my code that the item I need has "freddy" in the field "names".

I saw many forums mentioning "contains" but none that show an example...

My current code is the following:

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('users_table')
data = table.query(
    FilterExpression: 'names = :name',
    ExpressionAttributeValues: {
      ":name": "freddy"
    }
)

I obviously cannot use that because "names" is a list and not a string field. How can I look for "freddy" in names?

1 Answer 1

3

Since names field isn't part of the primary key, so you can't use query. The only way to look for an item by names is to use scan.

import boto3
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('users_table')
data = table.scan(
    FilterExpression=Attr('names').contains('freddy')
)
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this and i’m now getting Attr is not defined...
[ERROR] NameError: name 'Attr' is not defined
from boto3.dynamodb.conditions import Key, Attr

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.