Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find all items where a list field contains a value in dynamodb

I'm new to DynamoDb and I'm struggling to work out how to do this (using the java sdk).

I currently have a table (in mongo) for notifications. The schema is basically as follows (I've simplified it)

id: string
notifiedUsers: [123, 345, 456, 567]
message: "this is a message"
created: 12345678000 (epoch millis)

I wanted to migrate to Dynamodb, but I can't work out the best way to select all notifications that went to a particular user after a certain date?

I gather I can't have an index on a list like notifiedUsers, therefore I can't use a query in this case - is that correct?

I'd prefer not to scan and then filter, there could be a lot of records.

Is there a way to do this using a query or another approach?

EDIT This is what I'm trying now, it's not working and I'm not sure where to take it (if anywhere).

Condition rangeKeyCondition = new Condition()
    .withComparisonOperator(ComparisonOperator.CONTAINS.toString())
    .withAttributeValueList(new AttributeValue().withS(userId));

if(startTimestamp != null) {
  rangeKeyCondition = rangeKeyCondition.withComparisonOperator(ComparisonOperator.GT.toString())
      .withAttributeValueList(new AttributeValue().withS(startTimestamp));
}

NotificationFeedDynamoRecord replyKey = new NotificationFeedDynamoRecord();
replyKey.setId(partitionKey);

DynamoDBQueryExpression<NotificationFeedDynamoRecord> queryExpression = new DynamoDBQueryExpression<NotificationFeedDynamoRecord>()
    .withHashKeyValues(replyKey)
    .withRangeKeyCondition(NOTIFICATIONS, rangeKeyCondition);
like image 302
mark Avatar asked Oct 31 '22 13:10

mark


1 Answers

In case anyone else comes across this question, in the end we flattened the schema, so that there is now a record per userId. This has lead to problems because it's not possible with dynamoDb to atomically batch write records. With the original schema we had one record, and could write it atomically ensuring that all users got that notification. Now we cannot be certain, and this is causing pain.

like image 133
mark Avatar answered Nov 15 '22 05:11

mark