> For the complete documentation index, see [llms.txt](https://docs.mongoing.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mongoing.com/indexes/text-indexes/limit-the-number-of-entries-scanned.md).

# 限制扫描条目的数量

本教程描述了如何创建索引来限制对包含[`$text`](https://docs.mongodb.com/master/reference/operator/query/text/#op._S_text)表达式和相等条件的查询扫描的索引条目的数量。

集合`inventory`包含以下文档：

```
{ _id: 1, dept: "tech", description: "lime green computer" }
{ _id: 2, dept: "tech", description: "wireless red mouse" }
{ _id: 3, dept: "kitchen", description: "green placemat" }
{ _id: 4, dept: "kitchen", description: "red peeler" }
{ _id: 5, dept: "food", description: "green apple" }
{ _id: 6, dept: "food", description: "red potato" }
```

考虑由各个部门执行文本搜索的通用用例，例如:

```
db.inventory.find( { dept: "kitchen", $text: { $search: "green" } } )
```

为了限制文本搜索只扫描特定部门内的那些文档，创建一个复合索引，首先在字段`dept`上指定一个升序/降序索引键，然后在字段描述上指定一个文本索引键:

```
db.inventory.createIndex(
   {
     dept: 1,
     description: "text"
   }
)
```

然后，特定部门内的文本搜索将限制索引文档的扫描。例如，下面的查询只扫描那些**dept = kitchen**的文档:

```
db.inventory.find( { dept: "kitchen", $text: { $search: "green" } } )
```

> **\[success] 注意**
>
> * 复合`text`索引不能包含任何其他特殊索引类型，例如[多键](https://docs.mongodb.com/master/core/index-multikey/#index-type-multi-key)或 [地理空间](https://docs.mongodb.com/master/geospatial-queries/#index-feature-geospatial)索引字段。
> * 如果复合`text`索引在 索引键之前包含键，则要`text`执行[`$text`](https://docs.mongodb.com/master/reference/operator/query/text/#op._S_text)搜索，查询谓词必须在前面的键上包含**相等匹配条件**。
> * 创建复合`text`索引时，所有`text`索引键必须在索引规范文档中相邻列出。

也可以看看

[文字索引](https://docs.mongodb.com/master/core/index-text/)

译者：杨帅


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mongoing.com/indexes/text-indexes/limit-the-number-of-entries-scanned.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
