-
Notifications
You must be signed in to change notification settings - Fork 13
Description
The current implementation of the fsxa-api library does not support several standard MongoDB query operators, including $elemMatch and $exists.
This significantly limits the ability to express complex filter logic in CaaS queries. Specifically, it is currently impossible to apply $elemMatch.
Example Use Case:
I want to build a query that does the following:
- Check if a field (e.g.,
formData.tt_permissions.value) exists - If it exists, apply
$elemMatchwith$inon its contents
This is not possible currently because:
-
Using
$elemMatchas an operator directly in the FSXA query builder results in:Unknown operator passed
-
Using
$existsis also unsupported but one can use$eqwith thenullvalue instead, which works similarly. -
I approached the problem using
QueryBuilderQuerylike this:function getTypePermissionsFilter(type: PermissionType, typePermissions: Set<string> | Array<string>) { const permissionFieldName = permissionHelper.mapPermissionTypeToFieldName(type); return { operator: LogicalQueryOperatorEnum.OR, filters: [ { operator: ComparisonQueryOperatorEnum.EQUALS, field: `formData.tt_permissions.value.formData.${permissionFieldName}`, value: null }, { operator: ComparisonQueryOperatorEnum.EQUALS, field: `formData.tt_permissions.value.formData.${permissionFieldName}.value`, value: [] }, { operator: '$elemMatch', field: `formData.tt_permissions.value.formData.${permissionFieldName}.value`, value: { 'label': { '$in': [...typePermissions] } } } ] } as LogicalFilter; }
-
This problem can be solved with the filter CaaS query directly inside the URL. Here is the example for query by
lt_rolepermission type:{ "$or":[ { "page.formData.tt_permissions.value.formData.lt_role":{ "$eq":null } }, { "page.formData.tt_permissions.value.formData.lt_role.value":{ "$eq":[] } }, { "page.formData.tt_permissions.value.formData.lt_role.value":{ "$elemMatch":{ "label":{ "$in":[ "VDs", "Direct Sales" ] } } } } ] }
Why This Matters:
These operators are part of the standard MongoDB query language and are crucial for:
- Safely querying optional fields
- Filtering documents by matching values in nested arrays or objects
- Preventing runtime errors due to accessing undefined properties
Requested Enhancement:
- Add support for
$elemMatchoperator in the FSXA-API query builder. - Optionally: expose a way to pass raw MongoDB filters to allow more advanced use cases without being constrained by the internal operator mapping logic.