Specify Name for text Index指定text索引的名称

On this page本页内容

Changed in MongoDB 4.2

Starting in version 4.2, for featureCompatibilityVersion set to "4.2" or greater, MongoDB removes the Index Name Length limit of 127 byte maximum. 从4.2版开始,对于设置为“4.2”或更高版本的featureCompatibilityVersion,MongoDB删除了最大127字节的索引名长度限制。In previous versions or MongoDB versions with featureCompatibilityVersion (fCV) set to "4.0", index names must fall within the limit.featureCompatibilityVersion(fCV)设置为“4.0”的早期版本或MongoDB版本中,索引名必须在限制范围内。

The default name for the index consists of each indexed field name concatenated with _text. 索引的默认名称由每个与_text连接的索引字段名组成。For example, the following command creates a text index on the fields content, users.comments, and users.profiles:例如,下面的命令在contentusers.commentsusers.profiles字段上创建文本索引:

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

The default name for the index is:索引的默认名称为:

"content_text_users.comments_text_users.profiles_text"

Specify a Name for text Index指定text索引的名称

You can pass the name option to the db.collection.createIndex() method:您可以将name选项传递给db.collection.createIndex()方法:

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

Use the Index Name to Drop a text Index使用索引名删除text索引

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.dropIndex()方法。

For example, consider the index created by the following operation:例如,考虑以下操作创建的索引:

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

db.collection.dropIndex("MyTextIndex")

To get the names of the indexes, use the db.collection.getIndexes() method.要获取索引的名称,请使用db.collection.getIndexes()方法。