How do you query for a non-existent (null) attribute in DynamoDB

后端 未结 2 1631
情话喂你
情话喂你 2020-12-16 09:04

I\'m trying to query a DynamoDB table to find all items where the email attribute is not set. A global secondary index called EmailPasswordIndex ex

相关标签:
2条回答
  • 2020-12-16 09:55

    @jaredHatfield is correct if the field does not exist but that will not work if the filed is null. NULL is a keyword and can't used directly. But you can use it with ExpressionAttributeValues.

    const params = {
        TableName: "Accounts",
        FilterExpression: "attribute_not_exists(email) or email = :null",
        ExpressionAttributeValues: {
            ':null': null
        }
    }
    
    dynamodb.scan(params, (err, data) => {
        if (err)
            console.log(JSON.stringify(err, null, 2));
        else
            console.log(JSON.stringify(data, null, 2));
    })
    
    0 讨论(0)
  • 2020-12-16 09:57

    DynamoDB's Global Secondary Indexes allow for the indexes to be sparse. That means that if you have a GSI whose hash or range key for an item is not defined then that item will simply not be included in the GSI. This is useful in a number of use cases as it allows you to directly identify records that contain certain fields. However, this approach will not work if you are looking for the lack of a field.

    To get all of the items that have a field not set your best bet may be resorting to a scan with a filter. This operation will be very expensive but it would be straightforward code looking something like the following:

    var params = {
        TableName: "Accounts",
        FilterExpression: "attribute_not_exists(email)"
    };
    
    dynamodb.scan(params, {
        if (err)
            console.log(JSON.stringify(err, null, 2));
        else
            console.log(JSON.stringify(data, null, 2));
    });
    
    0 讨论(0)
提交回复
热议问题