#
#
Introduction
Modifiers are used to update documents declaratively, allowing operations similar to those in MongoDB. They can be applied to individual documents or entire collections.
Modifiers can also be used in a fluent writing style. Read more.
#
Setters vs Modifiers
Modifiers work as a set of instructions that describe how data should be changed, without the need to specify a particular document.
// Setter
final doc = Document({'name': 'John', 'age': 30});
doc.set('/age', 31); // Update value in 'age' property from document
// Modifier
final update = 'name'.rename('fullName'); // Create a modifier to rename 'name' to 'fullName'
update.apply(doc);
The setter acts directly on the property of a single document, while the modifier describes the transformation that should be applied in the future to a document or a collection, and can also be combined with filters.
#
Types of modifiers
CollQL modifiers can be grouped into the following categories:
- Value modifiers
- List modifiers
- Conditional modifiers
#
Value modifiers
Value modifiers are used to change the value of a property. The following value modifiers are supported:
final modifier = 'name'.set('John');
// or //
final modifier = update('dueDate').currentDate();
#
List modifiers
List modifiers are used to manipulate lists in a property. The following list modifiers are supported:
final modifier = 'colors'.push('red');
// or //
final modifier = update('positions').sort(18);
#
Conditional modifiers
Conditional modifiers are used to change the value of a property based on a condition. The following conditional modifiers are supported:
Usage examples:
final modifier = 'score'.max(100);
#
Extending modifiers
You can create your own modifiers by extending the Modifier class. This allows you to add new functionality or modify the behavior of existing modifiers. See Extending Filters for an idea of how to create new modifiers.