-
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
- Remove `$facet` usage from the aggregation pipeline.
- Implement an equivalent fallback logic that works when the match stage returns no documents.
- Ensure the final output remains identical to the previous behavior.
- Add or update tests covering both cases:
- when the match returns documents
- 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.