对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

createIndexes(数据库命令)

createIndexes

为集合构建一个或多个索引。

提示

In mongosh, this command can also be run through the db.collection.createIndex() and db.collection.createIndexes() helper methods.

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

The createIndexes command takes the following form:

db.runCommand(
{
createIndexes: <collection>,
indexes: [
{
key: {
<key-value_pair>,
<key-value_pair>,
...
},
name: <index_name>,
<option1>,
<option2>,
...
},
{ ... },
{ ... }
],
writeConcern: { <write concern> },
commitQuorum: <int|string>,
comment: <any>
}
)

The createIndexes command takes the following fields:

字段
类型
说明

createIndexes

字符串

要创建索引的集合。

indexes

阵列

指定要创建的索引。数组中的每个文档都指定一个单独的索引。

writeConcern

文档

可选。表达写关注的文档。省略以使用默认写关注。

commitQuorum

整数或字符串

可选。承载数据的副本集成员的最小数量(即提交法定节点数),包括主节点,必须在主节点将 indexes 标记为就绪之前报告索引构建成功。

当提交法定人数设置为默认 "votingMembers"时,您可以恢复一些 中断的索引构建。请参阅 setIndexCommitQuorum

提交法定人数中的副本集节点必须将 members[n].buildIndexes 设置为 true。如果任何投票节点将 members[n].buildIndexes设置为 false,则无法使用默认 "votingMembers" 提交法定人数。配置所有节点以将 members[n].buildIndexes 设置为 true,或者选择不同的提交法定人数。

支持以下值:

  • "votingMembers" - 所有承载数据的有投票权副本集成员(默认)。“投票”成员是 members[n].votes 大于 0 的任何副本集成员。

  • "majority" - 数据承载投票副本集成员的简单多数制。

  • <int> - 特定数量的承载数据的副本集节点。

  • 0 - 禁用法定人数投票行为。成员同时开始索引构建,但在完成索引构建之前投票或等待法定人数。如果您使用 0 的提交法定人数启动索引构建,则以后无法使用 setIndexCommitQuorum 修改提交法定人数。

  • 副本集标记名称

comment

any

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。

indexes 数组中的每个文档都可以采用以下字段:

字段
类型
说明

key

文档

指定索引的字段。对于每个字段,指定一个键值对,其中键是要索引的字段的名称,值是索引方向或索引类型。如果指定方向,请指定 1 为升序,或 -1 为降序。

MongoDB 支持多种不同的索引类型,包括:

有关更多信息,请参阅索引类型

通配符索引支持用户对自定义字段或集合中的大量字段进行查询的工作负载:

  • 您可以为特定字段及其子路径或文档中的所有字段创建通配符索引。

    有关详细信息,请参阅通配符索引。

name

字符串

唯一地标识索引的名称。

unique

布尔

可选。 创建唯一索引,以便集合不会接受索引键值与索引中现有值匹配的文档插入或更新。

指定 true 可创建唯一索引。默认值为 false

该选项不适用于哈希索引

partialFilterExpression

文档

可选。如果指定,索引只引用与过滤器表达式匹配的文档。有关更多信息,请参阅部分索引

过滤器表达式可以包括:

如果使用客户端字段级加密Queryable Encryption 加密,则 partialFilterExpression 不能引用加密字段。

您可以为所有 MongoDB索引类型指定 partialFilterExpression 选项。

sparse

布尔

可选。 如果为true,则索引仅引用具有指定字段的文档。这些索引使用的空间较少,但在某些情况下(尤其是排序),其行为会有所不同。 默认值为false 。有关更多信息,请参阅稀疏索引

以下索引类型默认为稀疏索引,忽略此选项:

对于包含 2dsphere 索引键以及其他类型的键的复合索引,只有 2dsphere 索引字段可确定索引是否引用文档。

MongoDB 提供了创建部分索引的选项。它们提供了稀疏索引功能的超集,因此是首选。

expireAfterSeconds

整型

可选。指定以秒为单位的生存时间 (TTL) 值,以便控制 MongoDB 在此集合中保留文档的时长。此选项仅会应用于 TTL 索引。请参阅通过设置 TTL 使集合中的数据过期以了解更多信息。

如果您使用 MongoDB 5.0 之前版本创建的 TTL 索引,或者要将 MongDB 5.0 创建的数据与之前版本同步,请参阅使用 NaN 配置索引,以避免错误配置问题。

TTL 索引 expireAfterSeconds 值必须介于 02147483647(包含两者)之间。

布尔

可选。一个标志,用于确定索引是否对查询规划器隐藏。选择查询计划时不会对隐藏索引进行评估。

默认值为 false

storageEngine

文档

可选。允许用户在创建索引时基于每个索引配置存储引擎。

storageEngine 选项应采用以下形式:

storageEngine: { <storage-engine-name>: <options> }

在复制过程中,对创建索引时指定的存储引擎配置选项进行验证并记录到 oplog 中,以支持包含使用不同存储引擎的成员的副本集。

weights

文档

可选。对于文本索引,指包含字段和权重对的文档。权重是一个介于 1 到 99,999 之间的整数,表示该字段相对于其他索引字段的得分重要性。您可以为部分或全部索引字段指定权重。请参阅为自管部署上的文本搜索结果分配权重来调整得分。默认值为 1

default_language

字符串

Optional. For text indexes, the language that determines the list of stop words and the rules for the stemmer and tokenizer. See Text Search Languages on Self-Managed Deployments for the available languages and Specify Language for Text Indexes on Self-Managed MongoDB for more information and examples. The default value is english.

language_override

字符串

Optional. For text indexes, the name of the field, in the collection's documents, that contains the override language for the document. The default value is language. See Specify Language for Text Indexes on Self-Managed MongoDB for an example.

textIndexVersion

整型

可选。text 索引版本号。用户可以使用此选项覆盖默认版本号。

有关可用版本,请参阅自管理部署上的文本索引版本。

2dsphereIndexVersion

整型

可选。2dsphere 索引版本号。用户可以使用此选项覆盖默认版本号。

有关可用版本,请参阅 2dsphere 索引。

bits

整型

可选。对于 2d 索引,存储的位置数据的地理哈希值的精度数。

bits 值的范围为 1 到 32(含)。默认值为 26

min

数字

可选。对于 2d 索引,这是经度和纬度值的包含下边界。默认值为 -180.0

max

数字

可选。对于 2d 索引,这是经度和纬度值的包含上边界。默认值为 180.0

collation

文档

可选。指定索引的排序规则

排序规则允许用户为字符串比较指定特定于语言的规则,例如字母大小写和重音符号规则。

如果在集合级别指定排序规则:

  • 如果创建索引时未指定排序规则,MongoDB 将使用集合的默认排序规则创建索引。

  • 如果您在创建索引时指定了排序规则,MongoDB 将使用指定的排序规则创建索引。

排序规则选项的语法如下:

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}

指定排序规则时,locale 字段为必填字段;所有其他排序规则字段均为可选字段。有关字段的说明,请参阅排序规则文档

wildcardProjection

文档

可选。

允许用户使用 { "$**" : 1} 键模式在通配符索引中包括或排除特定的字段路径。只有在所有文档字段上创建通配符索引时,该选项才有效。如果在特定字段路径及其子字段上创建通配符索引,则无法指定该选项,例如 { "path.to.field.$**" : 1 }

wildcardProjection 选项采用以下形式:

wildcardProjection: {
"path.to.field.a" : <value>,
"path.to.field.b" : <value>
}

<value> 可以是以下任一项:

  • 1true 以将字段包含在通配符索引中。

  • 0false 以将该字段包含在通配符索引中。

默认情况下,通配符索引会省略 _id 字段。要将 _id 字段包含在通配符索引中,必须将其显式包含在 wildcardProjection 文档中:

{
"wildcardProjection" : {
"_id" : 1,
"<field>" : 0|1
}
}

wildcardProjection 文档中的所有语句必须是包含语句或排除语句中的一种。您还可以在排除语句中包含 _id 字段。这是该规则的唯一例外。

mongosh provides the methods db.collection.createIndex() and db.collection.createIndexes() as wrappers for the createIndexes command.

MongoDB 不允许创建版本 0 索引。

The createIndexes command and mongosh helpers db.collection.createIndex() and db.collection.createIndexes() report an error if you create an index with one name, and then try to create the same index again but with another name.

{
"ok" : 0,
"errmsg" : "Index with name: x_1 already exists with a different name",
"code" : 85,
"codeName" : "IndexOptionsConflict"
}

在以前的版本中,MongoDB 不会再次创建索引,但会返回 ok 值为 1 的响应对象以及暗示未重新创建索引的注释。例如:

{
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}

注意

需要 featureCompatibilityVersion 4.4+

副本集或分片集群中的每个 mongod 必须featureCompatibilityVersion 设置为至少 4.4,才能跨副本集节点同时启动索引构建。

索引构建在副本集或分片集群上,并在所有数据承载副本集成员上同时构建。对于分片集群,索引构建仅会在包含当前被索引集合的数据的分片上进行。主节点需要最少数量的数据承载 voting 节点(即提交法定节点数),包括其自身,这些节点必须在将索引标记为可供使用之前完成构建。 有关详细信息,请参阅复制环境中的索引构建

要使用非默认提交法定人数启动索引构建,请指定 commitQuorum。

使用 setIndexCommitQuorum 命令修改正在进行的索引构建的提交法定节点数。

以下索引只支持简单的二进制比较,不支持排序规则

提示

要在具有非简单排序规则的集合上创建 text2d 索引,您必须在创建索引时显式指定 {collation: {locale: "simple"} }

使用 Stable API V1 时:

  • 您不能在 indexes 数组中指定以下任何字段:

    • background

    • bucketSize

    • sparse

    • storageEngine

  • 您不能创建文本索引。

  • 严格模式 查询规划 器会忽略上述不支持的索引类型。示例,尝试将sparse索引与cursor.hint()一起使用将导致以下BadValue错误:

    planner returned error :: caused by :: hint provided does not
    correspond to an existing index

For featureCompatibilityVersion "4.2", createIndexes uses an optimized build process that obtains and holds an exclusive lock on the specified collection at the start and end of the index build. All subsequent operations on the collection must wait until createIndexes releases the exclusive lock. createIndexes allows interleaving read and write operations during the majority of the index build.

对于 featureCompatibilityVersion"4.0" , 使用createIndexes pre-.4 2索引构建进程,该过程默认在整个构建进程中获得对父数据库的独占锁。.4 2前的构建进程会阻止对数据库及其所有集合的所有操作,直到该操作完成。background 索引未采用独占锁。

For more information on the locking behavior of createIndexes, see Index Builds on Populated Collections.

重要

If a data-bearing voting node becomes unreachable and the commitQuorum is set to the default votingMembers, index builds can hang until that node comes back online.

createIndexes支持在集合上构建一个或多个索引。createIndexes 使用内存和磁盘上的临时文件的组合来构建索引。默认内存限制为每个200 createIndexes命令 MB,由该命令中构建的所有索引平均共享。示例,如果使用一个10 createIndexes命令构建 索引,则在使用默认内存限制 时, MongoDB会为索引构建进程分配20 MB200 的索引。当达到内存限制时, MongoDB会在 内的_tmp --dbpath子目录中创建临时文件以完成构建。

使用 参数调整内存限制。仅在极少数情况下才需要增加此参数,例如当您运行单个maxIndexBuildMemoryUsageMegabytes 命令同时运行多个索引构建时,或者对大于createIndexes 500GB 的数据集索引时。

每个createIndexes 命令的限制为maxIndexBuildMemoryUsageMegabytes 。使用maxNumActiveUserIndexBuilds 的默认3 时,所有并发索引构建的总内存使用量最多可达3 值的maxIndexBuildMemoryUsageMegabytes 倍。

可以在不删除和重新创建索引的情况下更改隐藏选项。请参阅隐藏选项

Collation options on an existing index can be updated. To change other index options, drop the existing index with db.collection.dropIndex() then run createIndexes with the new options.

您可以使用不同的排序规则对同一键创建多个索引。 要创建具有相同键模式但不同排序规则的索引,您必须提供唯一索引名称。

如果在集合级别指定排序规则:

  • 如果创建索引时未指定排序规则,MongoDB 将使用集合的默认排序规则创建索引。

  • 如果您在创建索引时指定了排序规则,MongoDB 将使用指定的排序规则创建索引。

提示

通过指定排序规则 strength12,您可以创建不区分大小写的索引。排序规则 strength1 的索引既不区分变音符号,也不区分大小写。

要使用索引进行字符串比较,操作还必须指定相同的排序规则。换言之,如果一个操作对索引字段进行字符串比较,但又设定了与索引字段不同的排序规则,那么这个设有排序规则的索引将无法支持该操作。

警告

对于不带排序规则的索引,支持排序规则的索引键可能大于索引键,因为配置排序规则的索引使用 ICU 排序规则键来实现排序顺序。

To change the hidden option for existing indexes, you can use the following mongosh methods:

例如,

  • 要将索引的 hidden 选项更改为 true,请使用 db.collection.hideIndex() 方法:

    db.restaurants.hideIndex( { borough: 1, ratings: 1 } );
  • 要将索引的 hidden 选项更改为 false,请使用 db.collection.unhideIndex() 方法:

    db.restaurants.unhideIndex( { borough: 1, city: 1 } );
  • 默认情况下,通配符索引会省略 _id 字段。要将 _id 字段包含在通配符索引中,必须将其显式包含在 wildcardProjection 文档中:

    {
    "wildcardProjection" : {
    "_id" : 1,
    "<field>" : 0|1
    }
    }

    wildcardProjection 文档中的所有语句必须是包含语句或排除语句中的一种。您还可以在排除语句中包含 _id 字段。这是该规则的唯一例外。

  • 不支持通配符索引:

    通配符索引是稀疏(Sparse)索引。当索引字段不存在时,它们不支持查询。如果通配符字段的值为 null,则通配符索引将对文档进行索引。

    从 MongoDB 7.0 开始,通配符索引支持升序 (1) 和降序 (-1) 排序顺序。早期版本仅支持升序。

要了解详情,请参阅:

如果分布式事务不是跨分片写入事务,则可以在该事务中创建集合和索引。

To use createIndexes in a transaction, the transaction must use read concern "local". If you specify a read concern level other than "local", the transaction fails.

提交法定人数写关注之间有重要区别:

  • 索引构建使用提交法定人数。

  • 写入操作使用写关注。

集群中的每个承载数据的节点均为一个有投票权成员。

提交法定人数指定了必须准备多少个承载数据的有投票权节点,或是哪些有投票权节点(包括主节点)来提交同步索引构建。主节点才会执行提交。

写关注是指确认写入操作已传播到指定数量的实例的级别。

Changed in version 8.0:

提交法定人数指定了在主节点提交索引构建之前必须有多少个节点准备好完成索引构建。相反,当主节点已提交索引构建时,写关注则指定了在此命令返回成功之前有多少个节点必须复制索引构建操作日志条目。

在以前的版本中,当主节点提交索引构建时,写关注会指定在此命令返回成功之前有多少个节点必须完成索引构建。

以下命令会在 products 数据库的 inventory 集合上构建两个索引:

db.getSiblingDB("products").runCommand(
{
createIndexes: "inventory",
indexes: [
{
key: {
item: 1,
manufacturer: 1,
model: 1
},
name: "item_manufacturer_model",
unique: true
},
{
key: {
item: 1,
supplier: 1,
model: 1
},
name: "item_supplier_model",
unique: true
}
],
writeConcern: { w: "majority" }
}
)

在索引成功完成构建时,MongoDB 返回一个结果文档,其中包含 "ok" : 1 状态。

注意

有关“通配符索引”的完整文档,请参阅“通配符索引”。

下面列出了创建通配符索引的示例:

以某一 products_catalog 集合为例,其中的文档可能包含 product_attributes 字段。product_attributes 字段可包含任意嵌套字段,其中包括嵌入式文档文档和数组:

db.products_catalog.insertMany( [
{
_id : ObjectId("5c1d358bf383fbee028aea0b"),
product_name: "Blaster Gauntlet",
product_attributes: {
price: {
cost: 299.99,
currency: "USD"
}
}
},
{
_id: ObjectId("5c1d358bf383fbee028aea0c"),
product_name: "Super Suit",
product_attributes: {
superFlight: true,
resistance: [ "Bludgeoning", "Piercing", "Slashing" ]
}
}
] )

以下操作在 product_attributes 字段上创建一个通配符索引:

use inventory
db.runCommand(
{
createIndexes: "products_catalog",
indexes: [
{
key: { "product_attributes.$**" : 1 },
name: "wildcardIndex"
}
]
}
)

通过此通配符索引,MongoDB 可以对 product_attributes 的所有标量值建立索引。如果字段是嵌套文档或数组,则通配符索引将递归到文档/数组中,并为文档/数组中的所有标量字段建立索引。

通配符索引可以支持对 product_attributes 或其嵌套字段之一进行任意单字段查询:

db.products_catalog.find( { "product_attributes.superFlight" : true } )
db.products_catalog.find( { "product_attributes.maxSpeed" : { $gt : 20 } } )
db.products_catalog.find( { "product_attributes.elements" : { $eq: "water" } } )

注意

特定于路径的通配符索引语法与 wildcardProjection 选项不兼容。有关更多信息,请参阅参数文档

以某一 products_catalog 集合为例,其中的文档可能包含 product_attributes 字段。product_attributes 字段可包含任意嵌套字段,其中包括嵌入式文档文档和数组:

db.products_catalog.insertMany( [
{
_id : ObjectId("5c1d358bf383fbee028aea0b"),
product_name: "Blaster Gauntlet",
product_attributes: {
price: {
cost: 299.99,
currency: "USD"
}
}
},
{
_id: ObjectId("5c1d358bf383fbee028aea0c"),
product_name: "Super Suit",
product_attributes: {
superFlight: true,
resistance: [ "Bludgeoning", "Piercing", "Slashing" ]
}
}
] )

以下操作对所有标量字段(不包括 _id 字段)创建通配符索引:

use inventory
db.runCommand(
{
createIndexes: "products_catalog",
indexes: [
{
key: { "$**" : 1 },
name: "wildcardIndex"
}
]
}
)

通过此通配符索引,MongoDB 可为集合中每个文档的所有标量字段建立索引。如果给定字段是嵌套文档或数组,则通配符索引将递归到文档/数组中并对文档/数组中的所有标量字段构建索引。

创建的索引可以支持对集合中文档内任意字段的查询:

db.products_catalog.find( { "product_price" : { $lt : 25 } } )
db.products_catalog.find( { "product_attributes.elements" : { $eq: "water" } } )

注意

默认情况下,通配符索引会省略 _id 字段。要将 _id 字段包含在通配符索引中,则必须将其明确包含在wildcardProjection文档中。查看参数文档以获取更多信息。

以某一 products_catalog 集合为例,其中的文档可能包含 product_attributes 字段。product_attributes 字段可包含任意嵌套字段,其中包括嵌入式文档文档和数组:

db.products_catalog.insertMany( [
{
_id : ObjectId("5c1d358bf383fbee028aea0b"),
product_name: "Blaster Gauntlet",
product_attributes: {
price: {
cost: 299.99,
currency: "USD"
}
}
},
{
_id: ObjectId("5c1d358bf383fbee028aea0c"),
product_name: "Super Suit",
product_attributes: {
superFlight: true,
resistance: [ "Bludgeoning", "Piercing", "Slashing" ]
}
}
] )

以下操作创建通配符索引并使用wildcardProjection选项,以仅在索引中包含product_attributes.elementsproduct_attributes.resistance字段的标量值。

use inventory
db.runCommand(
{
createIndexes: "products_catalog",
indexes: [
{
key: { "$**" : 1 },
"wildcardProjection" : {
"product_attributes.elements" : 1,
"product_attributes.resistance" : 1
},
name: "wildcardIndex"
}
]
}
)

虽然键模式 "$**" 涵盖了文档中的所有字段,但 wildcardProjection 字段将索引限制为只有所包含的字段及其嵌套字段。

如果字段是嵌套文档或数组,则通配符索引将递归到文档/数组中并对文档/数组中的所有标量字段构建索引。

创建的索引可支持对 wildcardProjection 中包含的任何标量字段查询:

db.products_catalog.find( { "product_attributes.elements" : { $eq: "Water" } } )
db.products_catalog.find( { "product_attributes.resistance" : "Bludgeoning" } )

注意

通配符索引不支持在 wildcardProjection 文档中混用包含和排除语句,除非显示包含 _id 字段时。有关 wildcardProjection 的更多信息,请参阅参数文档

以某一 products_catalog 集合为例,其中的文档可能包含 product_attributes 字段。product_attributes 字段可包含任意嵌套字段,其中包括嵌入式文档文档和数组:

db.products_catalog.insertMany( [
{
_id : ObjectId("5c1d358bf383fbee028aea0b"),
product_name: "Blaster Gauntlet",
product_attributes: {
price: {
cost: 299.99,
currency: "USD"
}
}
},
{
_id: ObjectId("5c1d358bf383fbee028aea0c"),
product_name: "Super Suit",
product_attributes: {
superFlight: true,
resistance: [ "Bludgeoning", "Piercing", "Slashing" ]
}
}
] )

以下操作可创建通配符索引,并使用 wildcardProjection 文档对集合中每个文档的所有标量字段进行索引,不包括 product_attributes.elementsproduct_attributes.resistance 字段:

use inventory
db.runCommand(
{
createIndexes: "products_catalog",
indexes: [
{
key: { "$**" : 1 },
"wildcardProjection" : {
"product_attributes.elements" : 0,
"product_attributes.resistance" : 0
},
name: "wildcardIndex"
}
]
}
)

键模式"$**"涵盖文档中的所有字段,而wildcardProjection字段则从索引中排除指定字段。

如果字段是嵌套文档或数组,则通配符索引将递归到文档/数组中并对文档/数组中的所有标量字段构建索引。

创建的索引可以支持对任何标量字段的查询,但 wildcardProjection 排除的字段除外

db.products_catalog.find( { "product_attributes.maxSpeed" : { $gt: 25 } } )
db.products_catalog.find( { "product_attributes.superStrength" : true } )

注意

通配符索引不支持在 wildcardProjection 文档中混用包含和排除语句,除非显示包含 _id 字段时。有关 wildcardProjection 的更多信息,请参阅参数文档

注意

需要 featureCompatibilityVersion 4.4+

副本集或分片集群中的每个 mongod 必须featureCompatibilityVersion 设置为至少 4.4,才能跨副本集节点同时启动索引构建。

索引构建在副本集或分片集群上,并在所有数据承载副本集成员上同时构建。对于分片集群,索引构建仅会在包含当前被索引集合的数据的分片上进行。主节点需要最少数量的数据承载 voting 节点(即提交法定节点数),包括其自身,这些节点必须在将索引标记为可供使用之前完成构建。 有关详细信息,请参阅复制环境中的索引构建

To set the commit quorum, use createIndexes to specify the commitQuorum value.

commitQuorum 指定在主成员执行提交之前必须准备好多少个承载数据的投票成员或哪些投票成员(包括主节点)来提交索引构建。默认提交法定人数为 votingMembers,这意味着是所有承载数据的成员。

The following operation creates an index with a commit quorum of "majority", or a simple majority of data-bearing members:

db.getSiblingDB("examples").runCommand(
{
createIndexes: "invoices",
indexes: [
{
key: { "invoices" : 1 },
"name" : "invoiceIndex"
}
],
"commitQuorum" : "majority"
}
)

仅在简单多数承载数据的投票成员“投票”提交索引构建后,主节点才会将索引构建标记为准备就绪。有关索引构建和投票过程的更多信息,请参阅复制环境中的索引构建

The createIndexes command returns a document that indicates the success of the operation. The document contains some but not all of the following fields, depending on outcome:

createIndexes.createdCollectionAutomatically

如果为 true,则该集合不存在,并且是索引创建过程中创建的。

createIndexes.numIndexesBefore

命令开头的索引数。

createIndexes.numIndexesAfter

命令结尾的索引数。

createIndexes.ok

值为 1 表示索引已就位。值为 0 表示出现错误。

createIndexes.note

如果已存在一个或多个索引,则返回 note。这表示未创建或更改索引。

createIndexes.errmsg

返回有关任何错误的信息。

createIndexes.code

代表错误类型的错误代码。

以下代码区块说明了分片集群上的 createIndexes 输出示例。在分片集群上,createIndexes 输出 raw嵌入式文档,其中包含构建索引的每个分片的文档。raw嵌入式文档的键是分片ID以及组成分片的各个节点的主机名和端口的串联。

{
raw: {
'atlas-2m11gv-shard-1/atlas-2m11gv-shard-01-00.cpfgx.mongodb.net:27017,atlas-2m11gv-shard-01-01.cpfgx.mongodb.net:27017,atlas-2m11gv-shard-01-02.cpfgx.mongodb.net:27017': {
numIndexesBefore: 3,
numIndexesAfter: 5,
createdCollectionAutomatically: false,
commitQuorum: 'votingMembers',
ok: 1
},
'atlas-2m11gv-shard-0/atlas-2m11gv-shard-00-00.cpfgx.mongodb.net:27017,atlas-2m11gv-shard-00-01.cpfgx.mongodb.net:27017,atlas-2m11gv-shard-00-02.cpfgx.mongodb.net:27017': {
numIndexesBefore: 3,
numIndexesAfter: 5,
createdCollectionAutomatically: false,
commitQuorum: 'votingMembers',
ok: 1
}
},
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1743624296, i: 7 }),
signature: {
hash: Binary.createFromBase64('22j0GK8SIK806T+0OdCY6qYHocM=', 0),
keyId: Long('7438621020069560323')
}
},
operationTime: Timestamp({ t: 1743624296, i: 7 })
}