Search code examples
amazon-web-servicesamazon-dynamodbamazon-dynamodb-streams

Table has multiple records in which mobileNumber column is in only few records . I need to get those records only


Table has multiple records in which mobileNumber column is in only few records . I need to get those records only.

By this query i am getting all records.

$request = [
    'TableName' => 'tbl_camp',
    'ExpressionAttributeNames' => [
        '#mobileNumber' => 'mobileNumber'
    ],
    'ExpressionAttributeValues' => [
        ':val1' => ['S' => 'NULL']
    ],
    'FilterExpression' => '#mobileNumber <> :val1',
];

Solution

  • You can use attribute_exists() function to filter out items with whether certain attribute present.

    Your query becomes something like this:

    $request = [
        'TableName' => 'tbl_camp',
        'ExpressionAttributeNames' => [
            '#mobileNumber' => 'mobileNumber'
        ],
        'ExpressionAttributeValues' => [
            ':val1' => ['S' => 'NULL']
        ],
        'FilterExpression' => 'attribute_exists(#mobileNumber)',
    ];
    

    You can read about this at AWS documentation.