Is there a way to force MongoDB to filter first, then apply text search to that subset?

I want to filter documents by docname first (reducing dataset from ~100K to ~500 docs), then perform text search only on that filtered subset for better performance.

My Questions:
Is there a way to force MongoDB to filter first, then apply text search to that subset? Are there any aggregation pipeline techniques that could achieve filter-first behavior?

My Setup:

Collection: docs
Documents have fields: solution_id, docname, searchable (text content)
Current indexes: { searchable: “text” } (global text index)

Current Behavior:

Doc.where(docname: "TEXT SUMMARY").where( "$text: { $search: "floor plan details" }
)

The execution plan shows:
TEXT_MATCH stage (searches entire text index first)
FETCH stage with docname filter (filters results after text search)

Hi @Sai_06 , you can achieve this using the $search stage using the compound operator to specify a filter clause using the equals equals operator and use must clauses with the text operator. Query execution within the $search stage will be run such that filters are applied optimally.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.