new StringSetField()
A field that stores a set of string values. Behind the scenes this is backed by a DynamoDB string set. This field cannot be indexed but can be used in filter expressions.
Supports direct mutation operations that automatically track changes:
add(value)
- Add a string to the setdelete(value)
- Remove a string from the setclear()
- Remove all strings from the set
- Source
Example
// Creating with initial values
const doc = new MyModel({
tags: new Set(["javascript", "web", "tutorial"])
});
// Direct mutations (recommended - changes are auto-tracked)
doc.tags.add("advanced"); // Add a tag
doc.tags.delete("tutorial"); // Remove a tag
doc.tags.clear(); // Clear all tags
await doc.save(); // Changes are saved automatically
// Alternative: Replace entire set
doc.tags = new Set(["new", "tags"]);
await doc.save();
// Filter expressions
const results = await MyModel.scan({
filter: {
tags: { $contains: "javascript" }, // Contains specific value
tags: { $size: { $gt: 2 } } // Set size greater than 2
}
});