gman
gman

Reputation: 1272

NOT_NULL query condition on globalSecondaryIndex in dynamodb query

Is it possible to add constraint to a dynamodb query expression that states that a GSI should be not null?

Can somebody provide examples.

Is possible to construct a query like the one below?

new DynamoDBQueryExpression<XXX>()
            .withHashKeyValues(YYY).withKeyConditionExpression(GSI != NULL);

Note: Please let me know if this is possible in during query and not during filter time?

Upvotes: 1

Views: 5922

Answers (2)

if you're like me and you landed on this page while finding the answer to the above question, here's the thread you need to see

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

Upvotes: 2

notionquest
notionquest

Reputation: 39226

The DynamoDB String attribute can't have NULL or empty string.

When you try to insert NULL, the API should throw the below exception:-

java.lang.IllegalArgumentException: Input value must not be null

When you try to insert empty string, the API should throw the below exception:-

com.amazonaws.AmazonServiceException: One or more parameter values were invalid: An AttributeValue may not contain an empty string

If you want to add additional filters on some attributes (i.e. attributes other than hash or range key), you can use the below syntax (i.e. withFilterExpression).

Not equals operator is "<>"

Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
eav.put(":val1", new AttributeValue().withS("Some value"));

DynamoDBQueryExpression<XXX> queryExpression = new DynamoDBQueryExpression<XXX>();
    queryExpression.withHashKeyValues(hashKeyValues);       
    queryExpression.withFilterExpression("docType <> :val1").withExpressionAttributeValues(eav);

Upvotes: 0

Related Questions