Docs 主页 → 开发应用程序 → MongoDB Manual
为 text
索引 指定名称
注意
在 MongoDB 4.2 中进行了更改
从版本 4.2 开始,对于设置为"4.2"
或更大的featureCompatibilityVersion ,MongoDB 删除了最大 127 字节的索引名称长度限制。 在以前的版本或featureCompatibilityVersion (fCV) 设置为"4.0"
的 MongoDB 版本中,索引名称必须处于该限制之内。
索引的默认名称由每个已索引的字段名称与_text
连接组成。例如,以下命令在字段content
、 users.comments
和users.profiles
上创建text
索引:
db.collection.createIndex( { content: "text", "users.comments": "text", "users.profiles": "text" } )
索引的默认名称是:
"content_text_users.comments_text_users.profiles_text"
为text
索引指定名称
您可以将name
选项传递给db.collection.createIndex()
方法:
db.collection.createIndex( { content: "text", "users.comments": "text", "users.profiles": "text" }, { name: "MyTextIndex" } )
使用索引名称删除text
索引
无论文本索引是使用默认名称还是您为文本索引指定了名称,要删除文本索引,请将索引名称传递给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()
方法。