0
0
DynamodbHow-ToBeginner · 3 min read

How to Use size in Filter Expression in DynamoDB

In DynamoDB, use the size() function in a FilterExpression to filter items by the length of an attribute, such as a string or list. For example, FilterExpression: "size(attributeName) = :val" filters items where the attribute's size equals :val. This helps you select items based on attribute length during queries or scans.
📐

Syntax

The size() function returns the length of an attribute, which can be a string, list, map, or set. It is used inside a FilterExpression to filter items based on this length.

Basic syntax:

  • FilterExpression: "size(attributeName) operator :value"
  • attributeName: The name of the attribute to check size for.
  • operator: Comparison operator like =, <, >, <=, >=.
  • :value: A placeholder for the size value to compare against.
json
FilterExpression: "size(attributeName) = :val"
ExpressionAttributeValues: { ":val": { "N": "3" } }
💻

Example

This example shows how to scan a DynamoDB table to find items where the tags attribute (a list) has exactly 3 elements.

javascript
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB();

const params = {
  TableName: 'Products',
  FilterExpression: 'size(tags) = :tagCount',
  ExpressionAttributeValues: {
    ':tagCount': { N: '3' }
  }
};

dynamodb.scan(params, (err, data) => {
  if (err) {
    console.error('Error scanning:', err);
  } else {
    console.log('Items with 3 tags:', data.Items);
  }
});
Output
Items with 3 tags: [ { id: { S: '101' }, tags: { L: [ { S: 'red' }, { S: 'sale' }, { S: 'new' } ] } }, { id: { S: '102' }, tags: { L: [ { S: 'blue' }, { S: 'popular' }, { S: 'discount' } ] } } ]
⚠️

Common Pitfalls

  • Using size() on attributes that do not exist returns no match; ensure the attribute is present.
  • Comparing size() to a string value instead of a number causes errors; always use number type for size values.
  • Remember FilterExpression filters after fetching items; it does not reduce read capacity units consumed.
json
/* Wrong: comparing size to a string */
FilterExpression: 'size(name) = :val',
ExpressionAttributeValues: { ':val': { S: '5' } }  // Incorrect: should be number

/* Correct: comparing size to a number */
FilterExpression: 'size(name) = :val',
ExpressionAttributeValues: { ':val': { N: '5' } }
📊

Quick Reference

ConceptDescriptionExample
size(attribute)Returns length of string, list, map, or setsize(tags)
FilterExpressionFilters items after fetch based on conditionsize(tags) = :val
ExpressionAttributeValuesPlaceholder values for filter{ ':val': { N: '3' } }
Comparison OperatorsUse =, <, >, <=, >= with size()size(name) > :minLen

Key Takeaways

Use size() in FilterExpression to filter items by attribute length or size.
Always compare size() to a number, not a string, in ExpressionAttributeValues.
FilterExpression applies after fetching items and does not reduce read capacity usage.
size() works on strings, lists, maps, and sets in DynamoDB attributes.
Test your filter expressions to avoid common mistakes like missing attributes or wrong data types.