Workflow Filters
In CarePortals, you can fine-tune workflows by evaluating incoming data with filters on Triggers and Conditions. These filters use MongoDB query language to define rules that the incoming data, known as the payload, must meet.
How Filters Work
You can apply filters in two key places:
Triggers: A filter on a Trigger acts as a gatekeeper. The workflow will only start if the incoming data matches the filter's criteria. For example, you could trigger a workflow only if a new user is from a specific region.
Conditions: A filter in a Condition step creates a decision point within a running workflow. If the data meets the filter's criteria, the workflow proceeds along the workflow path. If not, it can be configured to jump to another step or exit the workflow.
For instance, if your workflow receives the following data payload:
{ "age": 14 } To ensure the workflow only proceeds for users who are 18 or older, your filter would be:
{ "age": { "$gte": 18 } } This query uses the $gte (greater than or equal to) operator to check if the value of the age field is 18 or more.
Operators
This list contains the most common and useful MongoDB queries for CarePortals trigger filtering:
$in: Matches documents where a field's value is present in a specified array.$nin: Matches documents where a field's value is not in a specified array.$exists: Checks for the presence or absence of a field.$gte: Matches values that are greater than or equal to a specified value.$gt: Matches values that are greater than a specified value.$lte: Matches values that are less than or equal to a specified value.$lt: Matches values that are less than a specified value.$eq: Matches values that are equal to a specified value.$ne: Matches values that are not equal to a specified value.$mod: Performs a modulo operation and matches documents based on the remainder.$all: Checks if an array field contains all specified elements.$and: Requires all query clauses to be true.$or: Requires at least one query clause to be true.$nor: Requires all query clauses to be false.$not: Inverts the effect of a query expression.$size: Matches documents where an array field has a specific number of elements.$type: Matches documents where a field is a particular BSON type.$regex: Uses regular expressions to match string patterns.$elemMatch: Checks if at least one element in an array matches all given criteria.
$in
The $in operator matches documents where the value of a field equals any value in the specified array.
You can filter for documents where the status is either "active" or "pending."
{ "status": { "$in": ["active", "pending"] } } $nin
The $nin operator matches documents where the value of a field is not in the specified array.
You can filter for documents where the status is not "draft" or "archived."
{ "status": { "$nin": ["draft", "archived"] } } $exists
The $exists operator matches documents that have the specified field. A value of true means the field must exist; false means it must not.
Example: Filter for documents that have an email field.
{ "email": { "$exists": true } } Greater and Lower Comparison Operators
These are value comparison operators.
$gtematches values greater than or equal to the specified value.$gtmatches values greater than the specified value.$ltematches values less than or equal to the specified value.$ltmatches values less than the specified value.
The example below filters for documents where the price is between 10 and 50 (inclusive).
{ "price": { "$gte": 10, "$lte": 50 } } $eq
The $eq operator matches documents where the value of a field is equal to the specified value.
The example below filters for documents where the category is exactly "electronics."
{ "category": { "$eq": "electronics" } } $ne
The $ne operator matches documents where the value of a field is not equal to the specified value.
The example below filters for documents where the status is not "archived."
{ "status": { "$ne": "archived" } } $mod
The $mod operator performs a modulo operation on the value of a field and matches documents where the remainder equals a specified value.
The example below filters for documents where the quantity is an even number.
{ "quantity": { "$mod": [2, 0] } } $all
The $all operator matches documents where an array field contains all the elements of the specified array.
The example below filters for documents where the tags array includes both "new" and "featured."
{ "tags": { "$all": ["new", "featured"] } } $and
The $and operator joins query clauses with a logical AND. All clauses must be true for the document to match.
The example below filters for documents where age is over 18 and status is "active."
{ "$and": [ { "age": { "$gt": 18 } }, { "status": "active" } ] } $or
The $or operator joins query clauses with a logical OR. At least one clause must be true for the document to match.
The example below filters for documents where the category is "clothing" or the price is less than 20.
{ "$or": [ { "category": "clothing" }, { "price": { "$lt": 20 } } ] } $nor
The $nor operator joins query clauses with a logical NOR. The document must fail to match all of the clauses.
The example below filters for documents where the price is not less than 20 and the status is not "on_sale."
{ "$nor": [ { "price": { "$lt": 20 } }, { "status": "on_sale" } ] } $not
The $not operator inverts the effect of a query expression and returns documents that do not match the expression.
The example below filters for documents where age is not greater than 18.
{ "age": { "$not": { "$gt": 18 } } } $size
The $size operator matches documents where an array field has a specific number of elements.
The example below filters for documents where the tags array has exactly 3 elements.
{ "tags": { "$size": 3 } } $type
The $type operator matches documents where a field is of a specific BSON type. You can use either the type number or the string alias.
The example below filters for documents where the name field is a string.
{ "name": { "$type": "string" } } $regex
The $regex operator provides the ability to use regular expressions to match string patterns.
The example below filters for documents where the name starts with "B."
{ "name": { "$regex": "^B" } } $elemMatch
The $elemMatch operator matches documents if an element in an array field matches all the specified query criteria.
The example below filters for documents where the items array contains at least one item with a name of "apple" and a quantity of 5.
{ "items": { "$elemMatch": { "name": "apple", "quantity": 5 } } }