Limit the Number of Entries Scanned
This tutorial describes how to create indexes to limit the number of index entries scanned for queries that includes a
$text expression and equality conditions.
inventory contains the following documents:
Consider the common use case that performs 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 first specifies an ascending/descending index key on the field
dept and then a
text index key on the field
Then, the text search within a particular department will limit the scan of indexed documents. For example, the following query scans only those documents with
dept equal to
- A compound
textindex cannot include any other special index types, such as multi-key or geospatial index fields.
- If the compound
textindex includes keys preceding the
textindex key, to perform a
$textsearch, the query predicate must include equality match conditions on the preceding keys.
- When creating a compound
textindex keys must be listed adjacently in the index specification document.