Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering / Querying by the Contents of a List in DynamoDB

I am attempting to filter a DynamoDB query by the contents of a Map contained within a List. Here's an example of the structure I'm dealing with.

{
    'EventType': 'git/push'
    'EventTime': 1416251010,
    'Commits': [
        {
            'id': '29d02aff...',
            'subject': 'Add the thing to the place'
        },
        {
            'id': '9d888fec...',
            'subject': 'Spelling errors'
        },
        ...
    ]
}

The hash key is EventType and range key EventTime. I am trying to write a filter that filters the result of a query to a specific id. Is is possible to create a DynamoDB filter expression that correctly filters the query like this? (My first thought was to use contains (a, a), but I don't think that will work on a List of Maps.)

like image 923
Tanaki Avatar asked Nov 17 '14 19:11

Tanaki


People also ask

How do I sort DynamoDB Query results?

DynamoDB uses the partition key as an input to the hash function. The hash function's output decides which partition the item will be placed in, and all items with the same partition key value are stored together. The sort key is used to sort and order items in a partition.

What is filtering in DynamoDB?

What is Filter Expression in DynamoDB? Filter Expression is a technique used in DynamoDB to filter data during scan or query operations. They are highly similar to WHERE clauses in SQL. However, they behave slightly differently because of the nature of NoSQL.

What does the Query operation in Amazon DynamoDB allow you to do?

The Query operation allows you to limit the number of items that it reads. To do this, set the Limit parameter to the maximum number of items that you want. For example, suppose that you Query a table, with a Limit value of 6 , and without a filter expression.


2 Answers

This isn't currently supported by DynamoDB's API's expression language (as of November 2014) but there are some workarounds mentioned here.

like image 188
Johnny Wu Avatar answered Oct 20 '22 13:10

Johnny Wu


I found a solution and I tested it, and it is working fine. Here's what I did,

// created a schema like that:

var Movie = dynamo.define('example-nested-attribute', {
  hashKey : 'title',
  timestamps : true,
  schema : {
    title       : Joi.string(),
    releaseYear : Joi.number(),
    tags        : dynamo.types.stringSet(),
    director    : Joi.object().keys({
      firstName : Joi.string(),
      lastName  : Joi.string(),
      titles    : Joi.array()
    }),
    actors : Joi.array().items(Joi.object().keys({
      firstName : Joi.string(),
      lastName  : Joi.string(),
      titles    : Joi.array()
    }))
  }
});

and then you can query of a nested array of objects:

  var params = {};
  params.UpdateExpression = 'SET #year = #year + :inc, #dir.titles = list_append(#dir.titles, :title), #act[0].firstName = :firstName ADD tags :tag';
  params.ConditionExpression = '#year = :current';
  params.ExpressionAttributeNames = {
    '#year' : 'releaseYear',
    '#dir' : 'director',
    '#act' : 'actors'
  };
  params.ExpressionAttributeValues = {
    ':inc' : 1,
    ':current' : 2001,
    ':title' : ['The Man'],
    ':firstName' : 'Rob',
    ':tag' : dynamo.Set(['Sports', 'Horror'], 'S')
  };
like image 1
hcurnor Avatar answered Oct 20 '22 13:10

hcurnor