4

I have two current tables

user table:

| user_id (primary key) | map of devices owned by this user |

event table:

| id_device (primary key) | time_stamp (sort key) | data column | map of users tied to this device e.g [ {user_id: "A"}, {user_id: "B"} ] |

user_id:1 could have a number of devices e.g 1,2,3,4 user_id:2 could also be able to modify the same devices and have different ones as well e.g 2,3,4,5

The event table is literally the latest event made by any device on the network, table grows larger as events occur.

I want to be able to query the latest events for devices by a user.

Method 1: I was originally going to use the dynamoDB API (BatchGetItem) and pass the map of devices as the keys. However, I cannot do this since I need to provide the time_stamps.

Is there a way to scan/query the "map of users tied to this device" column such as only filtering for the "A" user in the map? I know scanning not recommended

Method 2: Have a secondary index of only the id_device as a primary key. Query each if the latest events and go into the "map of users tied to this device" map to see if the owner is there. If so append to an empty array to be returned.

Does anyone else have any recommendations? Thanks!

1 Answer 1

2

Take a look at the available ComparisonOperators

Without copying the whole page, you can use these Comparison Operators

EQ | NE | LE | LT | GE | GT | NOT_NULL | NULL | CONTAINS | NOT_CONTAINS | BEGINS_WITH | IN | BETWEEN

And specifically you will probably want

CONTAINS :

Checks for a subsequence, or value in a set. AttributeValueList can contain only one AttributeValue element of type String, Number, or Binary (not a set type). If the target attribute of the comparison is of type String, then the operator checks for a substring match. If the target attribute of the comparison is of type Binary, then the operator looks for a subsequence of the target that matches the input. If the target attribute of the comparison is a set ("SS", "NS", or "BS"), then the operator evaluates to true if it finds an exact match with any member of the set. CONTAINS is supported for lists: When evaluating "a CONTAINS b", "a" can be a list; however, "b" cannot be a set, a map, or a list.

For example, given a device id, you can find all associated users like this:

var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'REGION'});

// Create DynamoDB service object
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

var params = {
 ExpressionAttributeValues: {
  ":device_id": {
    S: "A"
   }
 },
 FilterExpression: "contains (DeviceMapAttributeName, :device_id)",
 ProjectionExpression: "user_id",
 TableName: "USER_TABLE"
};

ddb.scan(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    data.Items.forEach(function(element, index, array) {
      console.log(element.user_id);
    });
  }
});

As you can see this is a scan. You cannot use a query in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.