翻译或纠错本页面

文本 索引指定名称

The default name for the index consists of each indexed field name concatenated with _text. For example, the following command creates a text index on the fields content, users.comments, and users.profiles:

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

索引的默认名称为:

"content_text_users.comments_text_users.profiles_text"

The text index, like other indexes, must fall within the index name length limit.

文本 索引指定名称

To avoid creating an index with a name that exceeds the index name length limit, you can pass the name option to the db.collection.createIndex() method:

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

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

Whether the text index has the default name or you specified a name for the text index, to drop the text index, pass the index name to the db.collection.dropIndex() method.

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

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

Then, to remove this text index, pass the name "MyTextIndex" to the db.collection.dropIndex() method, as in the following:

db.collection.dropIndex("MyTextIndex")

To get the names of the indexes, use the db.collection.getIndexes() method.