Dynamo db query using contains operator
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Amazon DynamoDB is a fully managed NoSQL database service provided by AWS. It's designed to deliver fast and predictable performance while seamlessly scaling from a startup to a global enterprise. One of the powerful features DynamoDB offers is its flexible querying capabilities. Among various query operators, the CONTAINS operator is often employed to pinpoint the existence of particular elements within data attributes, specifically within strings or sets. This article provides an in-depth examination of the CONTAINS operator, focusing on its application, technical considerations, and examples.
Using the CONTAINS Operator
The CONTAINS operator in DynamoDB allows you to search for the presence of a specified substring within a string attribute or an element within a collection (list or set). This is especially useful when you're interested in identifying whether an attribute includes a certain value, rather than matching the entire attribute.
Key Characteristics
- Applicable Data Types:
CONTAINScan be used for attributes of typeString,List, orSet. It cannot be used on number attributes. - Case Sensitivity: The operator is case-sensitive when applied to strings.
- Attribute Support: While it can be used on non-key attributes, it is not applicable to primary key attributes (partition key and sort key).
The Query Pattern
The query parameter utilizing the CONTAINS operator is generally structured within a FilterExpression to refine the results obtained from a broader query. This is because DynamoDB requires that queries operate primarily through a partition key and (optionally) a sort key, with filters narrowing down the results post-fetch.
Technical Example
Let's say you have a DynamoDB table called Products with the following attributes: ProductId, Category, Tags (Set<String>), and Description (String).
If you want to find products that have a description containing the word "portable" or include the tag "Featured", you could formulate the query as follows:
In this example, the FilterExpression uses the contains function twice: once to check if the Description includes "portable", and another to verify if the Tags set contains "Featured".
Detailed Use Case
Ecommerce Application
Consider an ecommerce application where customers can search for products using tags. Tags such as "Sale", "New Arrival", or "Exclusive" are stored in a set for each product in the Products table.
- Requirement: Retrieve all products tagged as "New Arrival".
- Implementing the Solution:
Performance Considerations
- Scans vs Queries: Note that using
containstypically necessitates a table scan which can be resource-intensive and may not be suitable for large tables without indexed attributes. - Indexing: If
containsmust be used on large datasets, consider using secondary indexes to improve performance. - Batch Process Filtering: Consider implementing server-side filtering logic to manage resource utilization efficiently, particularly in periodic batch processing tasks.
Summary Table
| Subject | Description |
| Data Types | Applicable to String, Set<String>, and List attributes |
| Use Case | Identify presence of a substring or element |
| Primary Key Usage | Not usable on partition key or sort key |
| Alternative | Use secondary indexes for efficiently managing large datasets |
| Case Sensitivity | The operator is case-sensitive when applied on string attributes |
| Filter Post-fetch | Commonly, contains is applied within a FilterExpression post primary key fetch |
| Performance | Be wary of scan operations on large tables due to potential performance hits |
Additional Considerations
- Cost Efficiency: Consider the cost implications of frequent scans, and use reserved capacity plans or on-demand settings optimally.
- Complex Queries: For complex data structures involving nested attributes, consider transforming data to a more query-able structure to make use of
contains. - Validation and Testing: Regularly validate and test filter expressions within a controlled environment to ensure efficient and expected application behavior.
By mastering the CONTAINS operator within DynamoDB's querying framework, developers can effectively search for pertinent data within highly dynamic and scalable data stores, all while managing performance, cost, and complexity.

