OPTIONS
翻译或纠错本页面

文本 索引指定名称

索引的默认名称由每个键名和 _text 拼接而成。例如,以下命令会在 content, users.commentsusers.profiles 键上创建一个 文本 索引:

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

索引的默认名称为:

"content_text_users.comments_text_users.profiles_text"

就像其他索引一样, 文本 索引夜必须遵守 index name length limit

文本 索引指定名称

为了避免创建的索引超出了 index name length limit 的限制,您可以在 db.collection.ensureIndex() 方法中传递 name 选项。

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

使用索引名称来删除 文本 索引

无论 文本 索引名称是默认的还是您重新指定的,如果希望删除 文本 索引,请在 db.collection.dropIndex() 方法中传递索引名称作为参数。

例如,假设由如下操作创建了一个索引:

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

那么,为了删除这条索引,可以将名称 "MyTextIndex" 作为参数传递给 db.collection.dropIndex() 方法,如下:

db.collection.dropIndex("MyTextIndex")

如果需要获得索引的名称,使用 db.collection.getIndexes() 命令。