指定 Literals 索引的名称

在本页面

索引的默认名称由与_text串联的每个索引字段名称组成。例如,以下命令在字段contentusers.commentsusers.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索引是默认名称还是您为text索引指定了名称,要删除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()方法。