On this page本页内容
createIndexes
Builds one or more indexes on a collection.在集合上生成一个或多个索引。
The createIndexes
command takes the following form:createIndexes
命令采用以下形式:
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
命令接受以下字段:
createIndexes | string | |
indexes | array | |
writeConcern | document | |
commitQuorum | integer or string |
|
comment | any |
|
Each document in the indexes
array can take the following fields:indexes
数组中的每个文档都可以采用以下字段:
key | document |
|
name | string | |
background | boolean |
|
unique | boolean |
|
partialFilterExpression | document |
|
sparse | boolean |
|
expireAfterSeconds | integer | |
hidden | boolean |
|
storageEngine | document |
storageEngine: { <storage-engine-name>: <options> }
|
weights | document | 1 . 1 。 |
default_language | string | english . english 。 |
language_override | string | language . language 。 |
textIndexVersion | integer |
|
2dsphereIndexVersion | integer |
|
bits | integer |
|
min | number | -180.0 . -180.0 。 |
max | number | 180.0 . 180.0 。 |
bucketSize | number |
|
collation | document |
collation: { locale: <string>, caseLevel: <boolean>, caseFirst: <string>, strength: <int>, numericOrdering: <boolean>, alternate: <string>, maxVariable: <string>, backwards: <boolean> }
|
wildcardProjection | document |
wildcardProjection: { "path.to.field.a" : <value>, "path.to.field.b" : <value> }
{ "wildcardProjection" : { "_id" : 1, "<field>" : 0|1 } }
|
mongosh
provides the methods 提供了方法db.collection.createIndex()
and db.collection.createIndexes()
as wrappers for the createIndexes
command.db.collection.createIndex()
和db.collection.createIndexes()
作为createIndexes
命令的包装器。
Changed in version 3.2.在版本3.2中更改。
Starting in version 4.2, for featureCompatibilityVersion set to 从4.2版开始,对于featureCompatibilityVersion设置为"4.2"
or greater, MongoDB removes the Index Name Length limit of 127 byte maximum. "4.2"
或更高版本,MongoDB删除了最大127字节的索引名称长度限制。In previous versions or MongoDB versions with featureCompatibilityVersion (fCV) set to 在featureCompatibilityVersion(fCV)设置为“4.0”的早期版本或MongoDB版本中,索引名称必须在限制范围内。"4.0"
, index names must fall within the limit.
Starting in version 4.2, the 从4.2版开始,如果您使用一个名称创建索引,然后尝试使用另一个名称再次创建相同的索引,则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.createIndexes
命令和mongosh
助手db.collection.createIndex()
和db.collection.createIndexes()
会报告错误。
{ "ok" : 0, "errmsg" : "Index with name: x_1 already exists with a different name", "code" : 85, "codeName" : "IndexOptionsConflict" }
In previous versions, MongoDB did not create the index again, but would return a response object with 在以前的版本中,MongoDB不会再次创建索引,但会返回一个ok
value of 1
and a note that implied that the index was not recreated. ok
值为1
的响应对象,以及一个表示未重新创建索引的注释。For Example:例如:
{ "numIndexesBefore" : 2, "numIndexesAfter" : 2, "note" : "all indexes already exist", "ok" : 1 }
Each 副本集或分片集群中的每个mongod
in the replica set or sharded cluster must have featureCompatibilityVersion set to at least 4.4
to start index builds simultaneously across replica set members.mongod
必须将featureCompatibilityVersion
设置为至少4.4
,才能在副本集成员之间同时启动索引构建。
MongoDB 4.4 running 运行featureCompatibilityVersion: "4.2"
builds indexes on the primary before replicating the index build to secondaries.featureCompatibilityVersion: "4.2"
的MongoDB 4.4在将索引构建复制到辅助数据库之前在主数据库上构建索引。
Starting with MongoDB 4.4, index builds on a replica set or sharded cluster build simultaneously across all data-bearing replica set members. 从MongoDB 4.4开始,索引在副本集上构建,或在所有数据承载副本集成员上同时构建分片集群。For sharded clusters, the index build occurs only on shards containing data for the collection being indexed. 对于分片集群,索引构建仅发生在包含索引集合数据的分片上。The primary requires a minimum number of data-bearing 初选需要最少数量的数据承载voting
members (i.e commit quorum), including itself, that must complete the build before marking the index as ready for use. voting
成员(即提交法定人数),包括其自身,这些成员必须在将索引标记为可使用之前完成构建。See Index Builds in Replicated Environments for more information.有关详细信息,请参阅复制环境中的索引生成。
To start an index build with a non-default commit quorum, specify the commitQuorum.要使用非默认提交仲裁启动索引生成,请指定commitQuorum
。
MongoDB 4.4 adds the MongoDB 4.4添加了setIndexCommitQuorum
command for modifying the commit quorum of an in-progress index build.setIndexCommitQuorum
命令,用于修改正在进行的索引构建的提交仲裁。
In MongoDB 4.2 and earlier, index builds on a replica set or sharded cluster build on the primary first before replicating to the secondaries. 在MongoDB 4.2和更早版本中,索引构建在副本集或分片集群上,首先构建在主集群上,然后再复制到辅助集群。See Index Builds In Replicated Environments (4.2) for the MongoDB 4.2 index build behavior.有关MongoDB 4.2索引构建行为,请参阅复制环境中的索引构建(4.2)。
To minimize the impact of building an index on replica sets and sharded clusters, use a rolling index build procedure as described on Rolling Index Builds on Replica Sets.要最大限度地减少在副本集和分片化集群上构建索引的影响,请使用滚动索引构建过程,如在副本集上滚动索引构建中所述。
The following indexes only support simple binary comparison and do not support collation:以下索引仅支持简单的二进制比较,不支持排序规则:
To create a 要在具有非简单排序规则的集合上创建text
, a 2d
, or a geoHaystack
index on a collection that has a non-simple collation, you must explicitly specify {collation: {locale: "simple"} }
when creating the index.text
、2d
或geoHaystack
索引,必须在创建索引时显式指定{collation: {locale: "simple"} }
。
When using Stable API V1:使用稳定API V1时:
You cannot specify any of the following fields in the 不能在indexes
array:indexes
数组中指定以下任何字段:
background
bucketSize
sparse
storageEngine
Changed in version 4.2.在版本4.2中更改。
For featureCompatibilityVersion 对于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. "4.2"
,createIndexes
使用一个优化的构建过程,该过程在索引构建的开始和结束时获取并保持对指定集合的独占锁。All subsequent operations on the collection must wait until 集合上的所有后续操作必须等待createIndexes
releases the exclusive lock. createIndexes
释放独占锁。createIndexes
allows interleaving read and write operations during the majority of the index build.createIndexes
允许在大多数索引构建过程中交错读取和写入操作。
For featureCompatibilityVersion 对于featureCompatibilityVersion "4.0"
, createIndexes
uses the pre-4.2 index build process which by default obtains an exclusive lock on the parent database for the entire duration of the build process. "4.0"
,createIndexes
使用4.2之前的索引构建过程,默认情况下,该过程在整个构建过程中获得父数据库的独占锁。The pre-4.2 build process blocks all operations on the database and all its collections until the operation completed. 4.2之前的构建过程会阻止对数据库及其所有集合的所有操作,直到操作完成。background
indexes do not take an exclusive lock.索引不采用独占锁。
For more information on the locking behavior of 有关createIndexes
, see Index Builds on Populated Collections.createIndexes
的锁定行为的更多信息,请参阅在填充集合上生成索引。
Changed in version 3.4.在版本3.4中更改。
createIndexes
createIndexes
createIndexes
is 200 megabytes (for versions 4.2.3 and later) and 500 (for versions 4.2.2 and earlier), shared between all indexes built using a single createIndexes
command. createIndexes
的默认内存使用限制为200兆字节(对于4.2.3及更高版本)和500兆字节(针对4.2.2及更低版本),在使用单个createIndexes
命令构建的所有索引之间共享。createIndexes
uses temporary disk files in a subdirectory named _tmp
within the --dbpath
directory to complete the build. createIndexes
将使用--dbpath
目录中名为_tmp
的子目录中的临时磁盘文件来完成构建。maxIndexBuildMemoryUsageMegabytes
server parameter. maxIndexBuildMemoryUsageMegabytes
服务器参数来覆盖内存限制。The hidden option can be changed without dropping and recreating the index. hidden
选项可以在不删除和重新创建索引的情况下进行更改。See Hidden Option.请参见Hidden
选项。
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.db.collection.dropIndex()
删除现有索引,然后使用新选项运行createIndexes
。
Collation
You can create multiple indexes on the same key(s) with different collations. 您可以使用不同的排序规则在同一个键上创建多个索引。To create indexes with the same key pattern but different collations, you must supply unique index names.要创建具有相同键模式但排序规则不同的索引,必须提供唯一的索引名称。
If you have specified a collation at the collection level, then:如果在集合级别指定了排序规则,则:
By specifying a collation 通过将排序规则strength
of 1
or 2
, you can create a case-insensitive index. strength
指定为1
或2
,可以创建不区分大小写的索引。Index with a collation 排序规则strength
of 1
is both diacritic- and case-insensitive.strength
为1
的索引不区分变音符号和大小写。
To use an index for string comparisons, an operation must also specify the same collation. 若要将索引用于字符串比较,操作还必须指定相同的排序规则。That is, an index with a collation cannot support an operation that performs string comparisons on the indexed fields if the operation specifies a different collation.也就是说,如果具有排序规则的索引指定了不同的排序规则,则该索引不能支持对索引字段执行字符串比较的操作。
For example, the collection 例如,集合myColl
has an index on a string field category
with the collation locale "fr"
.myColl
具有排序规则区域设置为"fr"
的字符串字段category
的索引。
db.myColl.createIndex( { category: 1 }, { collation: { locale: "fr" } } )
The following query operation, which specifies the same collation as the index, can use the index:以下查询操作指定与索引相同的排序规则,可以使用索引:
db.myColl.find( { category: "cafe" } ).collation( { locale: "fr" } )
However, the following query operation, which by default uses the "simple" binary collator, cannot use the index:但是,以下查询操作(默认使用“简单”二进制排序器)不能使用索引:
db.myColl.find( { category: "cafe" } )
For a compound index where the index prefix keys are not strings, arrays, and embedded documents, an operation that specifies a different collation can still use the index to support comparisons on the index prefix keys.对于索引前缀键不是字符串、数组和嵌入文档的复合索引,指定不同排序规则的操作仍然可以使用索引来支持对索引前缀键的比较。
For example, the collection 例如,集合myColl
has a compound index on the numeric fields score
and price
and the string field category
; the index is created with the collation locale "fr"
for string comparisons:myColl
在数字字段score
和price
以及字符串字段类别上有一个复合索引;索引是使用排序区域设置category
创建的,用于字符串比较:
db.myColl.createIndex( { score: 1, price: 1, category: 1 }, { collation: { locale: "fr" } } )
The following operations, which use 以下操作使用"simple"
binary collation for string comparisons, can use the index:"simple"
二进制排序规则进行字符串比较,可以使用索引:
db.myColl.find( { score: 5 } ).sort( { price: 1 } ) db.myColl.find( { score: 5, price: { $gt: NumberDecimal( "10" ) } } ).sort( { price: 1 } )
The following operation, which uses 以下操作使用"simple"
binary collation for string comparisons on the indexed category
field, can use the index to fulfill only the score: 5
portion of the query:"simple"
二进制排序规则对索引category
字段进行字符串比较,可以使用索引仅满足查询的score: 5
部分:
db.myColl.find( { score: 5, category: "cafe" } )
Hidden
New in version 4.4.在版本4.4中新增。
To hide an index, you must have featureCompatibilityVersion set to 要隐藏索引,必须将featureCompatibilityVersion设置为4.4或更高。4.4
or greater. However, once hidden, the index remains hidden even with featureCompatibilityVersion set to 然而,一旦隐藏,即使在MongoDB 4.4二进制文件上featureCompatibilityVersion设置为4.2
on MongoDB 4.4 binaries.4.2
,索引仍然隐藏。
To change the 要更改现有索引的hidden
option for existing indexes, you can use the following mongosh
methods:hidden
选项,可以使用以下mongosh
方法:
For Example,例如,
To change the 要将索引的hidden
option for an index to true
, use the db.collection.hideIndex()
method:hidden
选项更改为true
,请使用db.collection.hideIndex()
方法:
db.restaurants.hideIndex( { borough: 1, ratings: 1 } );
To change the 要将索引的hidden
option for an index to false
, use the db.collection.unhideIndex()
method:hidden
选项更改为false
,请使用db.collection.unhideIndex()
方法:
db.restaurants.unhideIndex( { borough: 1, city: 1 } );
New in version 4.2.在版本4.2中新增。
_id
field by default. _id
字段。_id
field in the wildcard index, you must explicitly include it in the wildcardProjection
document:_id
字段,必须在wildcardProjection
文档中明确包含它:{ "wildcardProjection" : { "_id" : 1, "<field>" : 0|1 } }
_id
field, you cannot combine inclusion and exclusion statements in the wildcardProjection
document._id
字段之外,您不能在wildcardProjection
文档中组合包含和排除语句。mongod
featureCompatibilityVersion must be 4.2
to create wildcard indexes. mongod
-featureCompatibilityVersion必须为4.2
才能创建通配符索引。Wildcard indexes do not support the following index types or properties:通配符索引不支持以下索引类型或属性:
Wildcard Indexes are distinct from and incompatible with Wildcard Text Indexes. 通配符索引不同于通配符文本索引,并且与通配符文本索引不兼容。Wildcard indexes cannot support queries using the 通配符索引不能支持使用$text
operator.$text
运算符的查询。
For complete documentation on wildcard index restrictions, see Wildcard Index Restrictions.有关通配符索引限制的完整文档,请参阅通配符索引限制。
For examples of wildcard index creation, see Create a Wildcard Index. 有关创建通配符索引的示例,请参阅创建通配符索引。 For complete documentation on Wildcard Indexes, see Wildcard Indexes.有关通配符索引的完整文档,请参阅通配符索引。
Changed in version 4.4.在版本4.4中更改。
Starting in MongoDB 4.4, you can create collections and indexes inside a multi-document transaction if the transaction is not a cross-shard write transaction.从MongoDB 4.4开始,如果事务不是跨分片写入事务,则可以在多文档事务中创建集合和索引。
To use 要在事务中使用createIndexes
in a transaction, the transaction must use read concern "local"
. createIndexes
,事务必须使用读取关注点"local"
。If you specify a read concern level other than 如果指定的读取关注级别不是"local"
, the transaction fails."local"
,则事务将失败。
The following command builds two indexes on the 以下命令在inventory
collection of the products
database: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" } } )
When the indexes successfully finish building, MongoDB returns a results document that includes a status of 当索引成功构建完成后,MongoDB返回一个结果文档,该文档的状态为"ok" : 1
."ok" : 1
。
New in version 4.2.在版本4.2中新增。 The mongod
featureCompatibilityVersion must be 4.2
to create wildcard indexes. mongod
-featureCompatibilityVersion
必须为4.2才能创建通配符索引。For instructions on setting the fCV, see Set Feature Compatibility Version on MongoDB 5.0 Deployments.有关设置fCV的说明,请参阅在MongoDB 5.0 Deployments上设置功能兼容性版本。
For complete documentation on Wildcard Indexes, see Wildcard Indexes.有关通配符索引的完整文档,请参阅通配符索引。
The following lists examples of wildcard index creation:以下列出了创建通配符索引的示例:
Consider a collection 考虑集合products_catalog
where documents may contain a product_attributes
field. products_catalog
,其中文档可能包含product_attributes
字段。The product_attributes
field can contain arbitrary nested fields, including embedded documents and arrays:product_attributes
字段可以包含任意嵌套字段,包括嵌入的文档和数组:
{ "_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" ] ... }, }
The following operation creates a wildcard index on the 以下操作在product_attributes
field:product_attributes
字段上创建通配符索引:
use inventory db.runCommand( { createIndexes: "products_catalog", indexes: [ { key: { "product_attributes.$**" : 1 }, name: "wildcardIndex" } ] } )
With this wildcard index, MongoDB indexes all scalar values of 使用此通配符索引,MongoDB为product_attributes
. product_attributes
的所有标量值编制索引。If the field is a nested document or array, the wildcard index recurses into the document/array and indexes all scalar fields in the document/array.如果字段是嵌套的文档或数组,则通配符索引将递归到文档/数组中,并为文档/数组的所有标量字段编制索引。
The wildcard index can support arbitrary single-field queries on 通配符索引可以支持product_attributes
or one of its nested fields: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" } } )
The path-specific wildcard index syntax is incompatible with the 路径特定的通配符索引语法与wildcardProjection
option. wildcardProjection
选项不兼容。See the parameter documentation for more information.有关详细信息,请参阅参数文档。
Consider a collection 考虑集合products_catalog
where documents may contain a product_attributes
field. products_catalog
,其中文档可能包含product_attributes
字段。The product_attributes
field can contain arbitrary nested fields, including embedded documents and arrays:product_attributes
字段可以包含任意嵌套字段,包括嵌入的文档和数组:
{ "_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" ] ... }, }
The following operation creates a wildcard index on all scalar fields (excluding the 以下操作在所有标量字段(不包括_id
field):_id
字段)上创建通配符索引:
use inventory db.runCommand( { createIndexes: "products_catalog", indexes: [ { key: { "$**" : 1 }, name: "wildcardIndex" } ] } )
With this wildcard index, MongoDB indexes all scalar fields for each document in the collection. 使用此通配符索引,MongoDB为集合中每个文档的所有标量字段编制索引。If a given field is a nested document or array, the wildcard index recurses into the document/array and indexes all scalar fields in the document/array.如果给定字段是嵌套的文档或数组,则通配符索引将递归到文档/数组中,并对文档/数组的所有标量字段进行索引。
The created index can support queries on any arbitrary field within documents in the collection:创建的索引可以支持对集合中文档中任意字段的查询:
db.products_catalog.find( { "product_price" : { $lt : 25 } } ) db.products_catalog.find( { "product_attributes.elements" : { $eq: "water" } } )
Wildcard indexes omit the 默认情况下,通配符索引忽略_id
field by default. _id
字段。To include the 要在通配符索引中包含_id
field in the wildcard index, you must explicitly include it in the wildcardProjection
document. _id
字段,必须在wildcardProjection
文档中显式包含它。See parameter documentation for more information.有关详细信息,请参阅参数文档。
Consider a collection 考虑集合products_catalog
where documents may contain a product_attributes
field. products_catalog
,其中文档可能包含product_attributes
字段。The product_attributes
field can contain arbitrary nested fields, including embedded documents and arrays:product_attributes
字段可以包含任意嵌套字段,包括嵌入的文档和数组:
{ "_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" ] ... }, }
The following operation creates a wildcard index and uses the 以下操作创建通配符索引,并使用wildcardProjection
option to include only scalar values of the product_attributes.elements
and product_attributes.resistance
fields in the index.wildcardProjection
选项在索引中仅包含product_attributes.elements
和product_attributes.resistance
字段的标量值。
use inventory db.runCommand( { createIndexes: "products_catalog", indexes: [ { key: { "$**" : 1 }, "wildcardProjection" : { "product_attributes.elements" : 1, "product_attributes.resistance" : 1 }, name: "wildcardIndex" } ] } )
While the key pattern 虽然键模式"$**"
covers all fields in the document, the wildcardProjection
field limits the index to only the included fields and their nested fields."$**"
覆盖了文档中的所有字段,但wildcardProjection
字段将索引限制为仅包含的字段及其嵌套字段。
If a field is a nested document or array, the wildcard index recurses into the document/array and indexes all scalar fields in the document/array.如果字段是嵌套的文档或数组,则通配符索引将递归到文档/数组中,并为文档/数组的所有标量字段编制索引。
The created index can support queries on any scalar field included in the 创建的索引可以支持对wildcardProjection
:wildcardProjection
中包含的任何标量字段的查询:
db.products_catalog.find( { "product_attributes.elements" : { $eq: "Water" } } ) db.products_catalog.find( { "product_attributes.resistance" : "Bludgeoning" } )
Wildcard indexes do not support mixing inclusion and exclusion statements in the 通配符索引不支持在wildcardProjection
document except when explicitly including the _id
field. wildcardProjection
文档中混合包含和排除语句,除非明确包含_id
字段。For more information on 有关wildcardProjection
, see the parameter documentation.wildcardProjection
的更多信息,请参阅参数文档。
Consider a collection 考虑集合products_catalog
where documents may contain a product_attributes
field. products_catalog
,其中文档可能包含product_attributes
字段。The product_attributes
field can contain arbitrary nested fields, including embedded documents and arrays:product_attributes
字段可以包含任意嵌套字段,包括嵌入的文档和数组:
{ "_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" ] ... }, }
The following operation creates a wildcard index and uses the 以下操作创建通配符索引,并使用wildcardProjection
document to index all scalar fields for each document in the collection, excluding the product_attributes.elements
and product_attributes.resistance
fields:wildcardProjection
文档为集合中每个文档的所有标量字段编制索引,不包括product_attributes.elements
和product_attributes.resistance
字段:
use inventory db.runCommand( { createIndexes: "products_catalog", indexes: [ { key: { "$**" : 1 }, "wildcardProjection" : { "product_attributes.elements" : 0, "product_attributes.resistance" : 0 }, name: "wildcardIndex" } ] } )
While the key pattern 虽然键模式"$**"
covers all fields in the document, the wildcardProjection
field excludes the specified fields from the index."$**"
覆盖文档中的所有字段,但wildcardProjection
字段将索引中的指定字段排除在外。
If a field is a nested document or array, the wildcard index recurses into the document/array and indexes all scalar fields in the document/array.如果字段是嵌套的文档或数组,则通配符索引将递归到文档/数组中,并为文档/数组的所有标量字段编制索引。
The created index can support queries on any scalar field except those excluded by 创建的索引可以支持对任何标量字段的查询,wildcardProjection
:wildcardProjection
排除的字段除外:
db.products_catalog.find( { "product_attributes.maxSpeed" : { $gt: 25 } } ) db.products_catalog.find( { "product_attributes.superStrength" : true } )
Wildcard indexes do not support mixing inclusion and exclusion statements in the 通配符索引不支持在wildcardProjection
document except when explicitly including the _id
field. wildcardProjection
文档中混合包含和排除语句,除非明确包含_id
字段。For more information on 有关wildcardProjection
, see the parameter documentation.wildcardProjection
的更多信息,请参阅参数文档。
Each 副本集或分片集群中的每个mongod
in the replica set or sharded cluster must have featureCompatibilityVersion set to at least 4.4
to start index builds simultaneously across replica set members.mongod
必须将featureCompatibilityVersion设置为至少4.4
,才能在副本集成员之间同时启动索引构建。
MongoDB 4.4 running 运行featureCompatibilityVersion: "4.2"
builds indexes on the primary before replicating the index build to secondaries.featureCompatibilityVersion: "4.2"
的MongoDB 4.4在将索引构建复制到辅助数据库之前在主数据库上构建索引。
Starting with MongoDB 4.4, index builds on a replica set or sharded cluster build simultaneously across all data-bearing replica set members. 从MongoDB 4.4开始,索引在副本集上构建,或在所有数据承载副本集成员上同时构建分片集群。For sharded clusters, the index build occurs only on shards containing data for the collection being indexed. 对于分片集群,索引构建仅发生在包含索引集合数据的分片上。The primary requires a minimum number of data-bearing 初选需要最少数量的数据承载voting
members (i.e commit quorum), including itself, that must complete the build before marking the index as ready for use. voting
成员(即提交法定人数),包括其自身,这些成员必须在将索引标记为可使用之前完成构建。See Index Builds in Replicated Environments for more information.有关详细信息,请参阅复制环境中的索引生成。
Specify the commitQuorum option to the 为createIndexes
operation to set the minimum number of data-bearing voting members (i.e commit quorum), including the primary, which must complete the index build before the primary marks the indexes as ready. createIndexes
操作指定commitQuorum
选项,以设置数据承载投票成员的最小数量(即提交仲裁),包括主成员,主成员必须在将索引标记为就绪之前完成索引构建。The default commit quorum is 默认的提交法定人数是votingMembers
, or all data-bearing voting replica set members.votingMembers
或所有数据承载的投票副本集成员。
The following operation creates an index with a commit quorum of 以下操作创建一个提交法定人数为"majority"
, or a simple majority of data-bearing voting members:"majority"
的索引,或包含投票成员数据的简单多数:
db.getSiblingDB("examples").runCommand( { createIndexes: "invoices", indexes: [ { key: { "invoices" : 1 }, "name" : "invoiceIndex" } ], "commitQuorum" : "majority" } )
The primary marks index build as ready only after a simple majority of data-bearing voting members "vote" to commit the index build. 只有经过简单多数的数据承载投票成员“投票”以提交索引构建后primary才会将索引构建标记为就绪。For more information on index builds and the voting process, see Index Builds in Replicated Environments.有关索引构建和投票过程的更多信息,请参阅复制环境中的索引构建。
The createIndexes
command returns a document that indicates the success of the operation. createIndexes
命令返回指示操作成功的文档。The document contains some but not all of the following fields, depending on outcome:根据结果,文档包含以下部分但不是全部字段:
createIndexes.createdCollectionAutomatically
If 如果为true
, then the collection didn't exist and was created in the process of creating the index.true
,则表示集合不存在,并且是在创建索引的过程中创建的。
createIndexes.ok
A value of 值1
indicates the indexes are in place. 1
表示索引已到位。A value of 值0
indicates an error.0
表示错误。