Record of dynamoDB OR status request?

I want to query a dynamodb table with a boolean or condition such as SQL for example Get me all the items where attribute1 = "no" or attribute2="no"

I tried with scanRequest.withScanFilter, but all conditions are met by doing logical ANDing. How to make logical ORing.?

+7
source share
3 answers

You can set the ConditionalOperator of your ScanRequest to "OR". The default value is AND

http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html

ScanRequest scanRequest = new ScanRequest("tableName");
scanRequest.setConditionalOperator(ConditionalOperator.OR);

Map<String, Condition> scanFilter = new HashMap<String, Condition>();
scanFilter.put("attribute1", new Condition().withAttributeValueList(new AttributeValue("no")).withComparisonOperator(ComparisonOperator.EQ));
scanFilter.put("attribute2", new Condition().withAttributeValueList(new AttributeValue("no")).withComparisonOperator(ComparisonOperator.EQ));

scanRequest.setScanFilter(scanFilter);
ScanResult scanResult = dynamo.scan(scanRequest);

for(Map<String, AttributeValue> item : scanResult.getItems()) {
    System.out.println(item);
}
+6
source

If you know the value HashKey, another option will use QUERY and FilterExpression. Here is an example with the Java SDK:

Table table = dynamoDB.getTable(tableName);

Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
expressionAttributeValues.put(":x", "no");
expressionAttributeValues.put(":y", "no");

QuerySpec spec = new QuerySpec()
    .withHashKey("HashKeyAttributeName", "HashKeyValueHere")
    .withFilterExpression("attribute1 = :x  or attribute2 = :y")
    .withValueMap(expressionAttributeValues);


ItemCollection<QueryOutcome> items = table.query(spec);

Iterator<Item> iterator = items.iterator();

while (iterator.hasNext()) {
    System.out.println(iterator.next().toJSONPretty());
}

. .

+5

You can also use parentheses in the FilterExpression expression:

const params = { TableName: process.env.PROJECTS_TABLE, IndexName: 'teamId-createdAt-index', KeyConditionExpression: 'teamId = :teamId', ExpressionAttributeValues: { ':teamId': verifiedJwt.teamId, ':userId': verifiedJwt.userId, ':provider': verifiedJwt.provider }, FilterExpression: 'attribute_exists(isNotDeleted) and ((attribute_not_exists(isPrivate)) or (attribute_exists(isPrivate) and userId = :userId and provider = :provider))' };

0
source

Source: https://habr.com/ru/post/1545005/


All Articles