JSON filters allow you to dynamically filter documents through configuration settings. There are two types of filters: global filters that are applied to every document and subsection-specific filters that are only applied to certain documents. You can also combine both filter types. This page explains how JSON filters work and how to configure them.

Global filter

A global filter is added to every MongoDB query that fetches documents. For example:

{
    "filter": {
        "query": "{\"tags\": {\"$nin\": [\"ignored_document\"]}}"
    },
    "subsections": {}
}

Adding this filter to the configuration will cause every document to be ignored if its tag collection contains the tag/value " ignored_document."

You can use any valid MongoDB query as a filter. Just make sure to properly escape the query to ensure the JSON remains valid.

Subsection specific filter

You can also set filters that are only applied to specific subsections or documents. The following filter shows you how to import only customers whose identifier starts with "1":

{
    "subsections": {
        "customer": {
            "filter": {
                "query": "{\"identifier\": {\"$regex\": \"^1\"}}"
            }
        }
    }
}

Another example: The following filter ignores all customers that are assigned to "customer_group_1".

{
    "subsections": {
        "customer": {
            "filter": {
                "query": "{\"customerInformation.customerGroup.identifier\": {\"$ne\": \"customer_group_1\"}}",
                "operator": "override_global"
            }
        }
    }
}

Combined Filters / Operators

A third option is to combine global and subsection-specific queries using operators. Three operators are available:

  • and: Both the subsection-specific query condition AND the global filter query condition must be true.
  • or: Either the subsection-specific query condition OR the global filter query condition must be true.
  • override_global: Only the subsection-specific query condition must be true. The global query is ignored.
{
    "filter": {
        "query": "{\"tags\": {\"$nin\": [\"ignored_document\"]}}"
    },
    "subsections": {
        "customer": {
            "filter": {
                "query": "{\"identifier\": {\"$regex\": \"^1\"}}"
            },
            "operator": "and|or|override_global"
        }
    }
}

Mapping-Extension

Filters can also be applied to the mapping extension. The principle is the same as with subsection-specific queries: you can define global queries and combine them with your mapping-specific queries using operators.

{
    "filter": {
        "query": "{\"tags\": {\"$nin\": [\"ignore_global\"]}}"
    },
    "extensions": {
        "Synqup\\Modules\\Shopware6MappingExtensionBundle\\Subscriber\\Shopware6MappingExtensionBundleSubscriber": {
            "mappings": [
                {
                    "fields": [],
                    "filter": {
                        "query": "{\"tags\": {\"$nin\": [\"ignore_custom\"]}}",
                        "operator": "and|or|override_global"
                    },
                    "targetTableName": "target_table_a",
                    "sourceDocumentFqcn": "Document_A"
                },
                {
                    "filter": {
                        "query": "{\"path.to.any.document.field\": {\"$ne\": \"some_value\"}}",
                        "operator": "and|or|override_global"
                    },
                    "targetTableName": "target_table_b",
                    "sourceDocumentFqcn": "Document_B"
                }
            ]
        }
    }
}