# db.collection.ensureIndex()

在本页面

* [定义](#definition)
* [附加信息](#additional-information)

## 定义

* `db.collection.` `ensureIndex`(键，选项)
  * *从3.0.0版开始不推荐使用：*`db.collection.ensureIndex()`现在是的别名 `db.collection.createIndex()`。

如果索引尚不存在，则在指定字段上创建索引。

## 附加信息

* 使用db.collection.createIndex()而不是db.collection.ensureIndex()来创建新索引。
* 本手册的索引部分用于 MongoDB 中索引和索引的完整文档。
* db.collection.getIndexes()查看集合的现有索引的规范。

译者：李冠飞

校对：


---

# Agent Instructions: 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/can-kao/mongo-shell-methods/collection-methods/db-collection-ensureindex.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.
