- Indexes >
- Indexing Tutorials >
- Text Search Tutorials >
- Limit the Number of Entries Scanned
Limit the Number of Entries Scanned¶
This tutorial describes how to limit the text search to scan only those documents with a field value.
The text command includes the filter option to further
restrict the results of a text search. For a filter that specifies
equality conditions, this tutorial demonstrates how to perform text
searches on only those documents that match the filter conditions,
as opposed to performing a text search first on all the documents and
then matching on the filter condition.
Consider a collection inventory that contains the following
documents:
A common use case is to perform text searches by individual departments, such as:
To limit the text search to scan only those documents within a specific
dept, create a compound index that specifies an
ascending/descending index key on the field dept and a text
index key on the field description:
Important
- The ascending/descending index keys must be listed before, or
prefix, the
textindex keys. - By prefixing the
textindex fields with ascending/descending index fields, MongoDB will only index documents that have the prefix fields. - You cannot include multi-key index fields or geospatial index fields.
- The
textcommand must include thefilteroption that specifies an equality condition for the prefix fields.
Then, the text search within a particular department will limit the
scan of indexed documents. For example, the following text
command scans only those documents with dept equal to kitchen:
The returned result includes the statistics that shows that the command
scanned 1 document, as indicated by the nscanned field:
For more information on the result set, see Output.