Reputation: 1401
I'm trying to query(getItems) on DynamoDB table to find all items where the InitiateMQFlag = 0 or the 'InitiateMQFlag' attribute is not yet set. A global secondary index called BLEAdvertisement-InitiateMQFlag-index exists on the table which includes the InitiateMQFlag field. I had used FilterExpression: "attribute_not_exists(InitiateMQFlag)" but it throws me errors.
var params = {
TableName: device_table,
IndexName: "BLEAdvertisement-InitiateMQFlag-index",
KeyConditionExpression: "BLEAdvertisement = :BLEAdvertisement AND InitiateMQFlag = :InitiateMQFlag",
ExpressionAttributeValues: {
':BLEAdvertisement': BLEAdvertisements[i],
':InitiateMQFlag' : InitiateMQFlagValue
},
FilterExpression: "attribute_not_exists(InitiateMQFlag)"
};
Result:
code:"ValidationException"
message:"Filter Expression can only contain non-primary key attributes: Primary key attribute: InitiateMQFlag"
name:"ValidationException"
requestId:"OVIAPBSCAAU42OI41LQUSGIU7JVV4KQNSO5AEMVJF66Q9ASUAAJG"
retryable:false
retryDelay:3.1105465830200685
statusCode:400
If there is another way to get the non-existent attribute with another query on single attribute, it will also help a lot.
Upvotes: 1
Views: 735
Reputation: 13731
A FilterExpression
is used to relatively-inefficiently filter out results after already reading them from the partition - you'll be paying for the read before the filtering happens. DynamoDB is refusing to allow you to filter on a primary key (partition key or sort key) to hint you that there is a more efficient way to do this: You should use a KeyConditionExpression
instead.
And, in your case, you are indeed doing a KeyConditionExpression
on the key column InitiateMQFlag. So beyond the fact that it's not allowed to filter on this column, why would you even want to? Given KeyConditionExpression
already limits the query only to items where InitiateMQFlag = :InitiateMQFlag - how can it ever match the attribute_not_exists(InitiateMQFlag) filter?
Upvotes: 2