diff --git a/integrations/mongodb_atlas/src/haystack_integrations/document_stores/mongodb_atlas/filters.py b/integrations/mongodb_atlas/src/haystack_integrations/document_stores/mongodb_atlas/filters.py index 46bc32b81..f8a9425a7 100644 --- a/integrations/mongodb_atlas/src/haystack_integrations/document_stores/mongodb_atlas/filters.py +++ b/integrations/mongodb_atlas/src/haystack_integrations/document_stores/mongodb_atlas/filters.py @@ -35,11 +35,6 @@ def _parse_logical_condition(condition: Dict[str, Any]) -> Dict[str, Any]: msg = f"'conditions' key missing in {condition}" raise FilterError(msg) - operator = condition["operator"] - if operator not in ["AND", "OR", "NOT"]: - msg = f"Unknown logical operator '{operator}'. Valid operators are: 'AND', 'OR', 'NOT'" - raise FilterError(msg) - # logical conditions can be nested, so we need to parse them recursively conditions = [] for c in condition["conditions"]: @@ -48,6 +43,7 @@ def _parse_logical_condition(condition: Dict[str, Any]) -> Dict[str, Any]: else: conditions.append(_parse_logical_condition(c)) + operator = condition["operator"] if operator == "AND": return {"$and": conditions} elif operator == "OR": @@ -57,6 +53,9 @@ def _parse_logical_condition(condition: Dict[str, Any]) -> Dict[str, Any]: # we combine $nor and $and to achieve the same effect. return {"$nor": [{"$and": conditions}]} + msg = f"Unknown logical operator '{operator}'. Valid operators are: 'AND', 'OR', 'NOT'" + raise FilterError(msg) + def _parse_comparison_condition(condition: Dict[str, Any]) -> Dict[str, Any]: field: str = condition["field"]