Refactor: Remove $facet usage in aggregation pipeline

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Python Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Context

      Our current aggregation relies on `$facet` only to ensure that the pipeline produces a result when the match stage returns zero documents. This adds unnecessary complexity and overhead. We can replace `$facet` with a simpler pattern (e.g., `unionWith` + dummy document or precomputed fields) without changing the semantics of the result.

      Definition of done

      1. Remove `$facet` usage from the aggregation pipeline.
      2. Implement an equivalent fallback logic that works when the match stage returns no documents.
      3. Ensure the final output remains identical to the previous behavior.
      4. Add or update tests covering both cases:
        1. when the match returns documents
        2. when the match returns an empty result

      Pitfalls

      • `$$NOW` and other system variables are not available inside nested pipelines like `$unionWith`; capture required system values before the union.
      • Ensure that dummy documents do not interfere with grouping or calculations.

            Assignee:
            Emanuel Lupi
            Reporter:
            Emanuel Lupi
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: