partialFilterExpression: what it accepts, and sizing the reduction
standardintermediatepartialFilterExpression accepts equality, $exists, comparison ($gt/$gte/$lt/$lte), $type, $and, $or, $in, and the geospatial $geoWithin/$geoIntersects operators — a real but not unlimited subset of the query language ($regex and several others are not supported). Size reduction is worth measuring, not assuming: count how many documents actually match the filter versus the full collection.
Think of it as
The operator restriction exists because the filter is evaluated once per document, at write and index-build time, to decide "does this document belong in the index" — not re-evaluated per query the way a normal find() filter is. That is still a real, useful subset of the query language (including $or and $in), just not every operator a find() call can use.
What we're doing: Measure a real size reduction rather than assuming one, using the $in support the earlier, narrower mental model would have missed.
- 1
- Measuring both counts directly gives the real reduction percentage, rather than assuming "pending orders are rare" without checking.
- 5
- $in is supported in partialFilterExpression, so the filter can be written directly rather than expanded into several $or branches.
Why this works: The whole point of a partial index is a size/cost reduction that is worth the complexity of remembering to include its filter condition in queries — that trade is only justified if the actual measured reduction is significant, which requires counting, not assuming.
Assuming partialFilterExpression's operator set from an outdated or half-remembered list
Wrong
Better
What you see: Effort spent rewriting a filter to avoid an operator that was actually supported all along, based on a stale or incomplete mental model of the restriction.
Why: MongoDB's supported-operator list for partialFilterExpression has expanded over versions — a claim about which operators are or are not supported is a version-specific fact worth re-checking against the current manual rather than carrying forward from memory.
Remember: partialFilterExpression supports equality, $exists, comparison operators, $type, $and, $or, $in, and geospatial operators — check the current manual before assuming an operator is unsupported. Measure the real size reduction (matching vs. total document count) rather than assuming it.
See also: sparse and partial indexes

