Docs 菜单

Docs 主页开发应用程序MongoDB Manual

text索引 指定名称

在此页面上

  • text索引指定名称
  • 使用索引名称删除text索引

注意

在 MongoDB 4.2 中进行了更改

从版本 4.2 开始,对于设置为"4.2"或更大的featureCompatibilityVersion ,MongoDB 删除了最大 127 字节的索引名称长度限制。 在以前的版本或featureCompatibilityVersion (fCV) 设置为"4.0"的 MongoDB 版本中,索引名称必须处于该限制之内。

索引的默认名称由每个已索引的字段名称与_text连接组成。例如,以下命令在字段contentusers.commentsusers.profiles上创建text索引:

db.collection.createIndex(
{
content: "text",
"users.comments": "text",
"users.profiles": "text"
}
)

索引的默认名称是:

"content_text_users.comments_text_users.profiles_text"

您可以将name选项传递给db.collection.createIndex()方法:

db.collection.createIndex(
{
content: "text",
"users.comments": "text",
"users.profiles": "text"
},
{
name: "MyTextIndex"
}
)

无论文本索引是使用默认名称还是您为文本索引指定了名称,要删除文本索引,请将索引名称传递给db.collection.dropIndex()方法。

例如,考虑以下操作创建的索引:

db.collection.createIndex(
{
content: "text",
"users.comments": "text",
"users.profiles": "text"
},
{
name: "MyTextIndex"
}
)

然后,要删除此文本索引,请将名称"MyTextIndex"传递给db.collection.dropIndex()方法,如下所示:

db.collection.dropIndex("MyTextIndex")

要获取索引的名称,请使用 db.collection.getIndexes()方法。

← 指定文本索引的语言