1

I have a table that has a column called "items", but not all rows have it, so I want to scan all rows that have "items".

Something like:

$resposta = $clientDB->scan(array(
    'TableName' => 'tableName',
    'Key' => [
        'items' => ['S' => 'exists']
    ]
));

But I can't figure out how to do it...

The table has 10000 rows, but only 10 of them have "items", and I want to get only these 10 rows.

Edit:

As Seth Geoghegan pointed below, it was necessary create a global secondary indexes on the attribute i wanted to filter.

I ended up here:

    $params = [
        'TableName' => 'tableName',
        'FilterExpression' => "attribute_exists(items)"
    ];

OR

    $params = [
        'TableName' => 'tableName',
        'FilterExpression' => 'items != :null',
        'ExpressionAttributeValues' =>  [
            ':null' => null,
        ],
    ];

But both didnt worked... First one seens necessary ExpressionAttributeValues to be setup and the second the php stop working with no error logs.

2 Answers 2

2

This is a perfect use case for global secondary indexes (GSI)!

You can create a GSI on the items attribute. Items with the items attribute defined will get projected into the GSI. Importantly, items that do not contain this attribute will not be in the index. You could then query the GSI and retrieve the items you're after.

Sign up to request clarification or add additional context in comments.

1 Comment

i will try that and see if's gonna work
1

Well, after some effort, i found a way though:

    $resposta = $clientDB->scan(array(
        'TableName' => 'tableName',
        'FilterExpression' => "attribute_exists(items)"
    ));

After i created another global secondary index (GSI) for "items" (pointed by Seth Geoghegan), i just needed to add in the scan function the FilterExpression the "attribute_exists(items") and it worked.

Comments

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.