On this page本页内容
db.collection.createIndexes( [ keyPatterns ], options, commitQuorum )
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
Creates one or more indexes on a collection.在集合上创建一个或多个索引。
db.collection.createIndexes()
takes the following parameters:采用以下参数:
keyPatterns | document |
|
options | document | |
commitQuorum | integer or string |
|
The db.collection.createIndexes()
is a wrapper around the createIndexes
command.db.collection.createIndexes()
是createIndexes
命令的包装器。
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 options
document contains a set of options that control the creation of the indexes. options
文档包含一组控制索引创建的选项。Different index types can have additional options specific for that type.不同的索引类型可以有特定于该类型的其他选项。
Multiple index options can be specified in the same document. 可以在同一文档中指定多个索引选项。However, if you specify multiple option documents the 但是,如果指定多个选项文档,db.collection.createIndexes()
operation will fail.db.collection.createIndexes()
操作将失败。
Consider the following 考虑以下db.collection.createIndexes()
operation:db.collection.createIndexes()
操作:
db.collection.createIndexes( [ { "a": 1 }, { "b": 1 } ], { unique: true, sparse: true, expireAfterSeconds: 3600 } )
If the options specification had been split into multiple documents like this: 如果将选项规范拆分为多个文档,如 { unique: true }, { sparse: true, expireAfterSeconds: 3600 }
the index creation operation would have failed.{ unique: true }, { sparse: true, expireAfterSeconds: 3600 }
,索引创建操作将失败。
When you specify options to 当您为db.collection.createIndexes()
, the options apply to all of the specified indexes. db.collection.createIndexes()
指定选项时,这些选项将应用于所有指定的索引。For example, if you specify a collation option, all of the created indexes will include that collation.例如,如果指定排序规则选项,则所有创建的索引都将包括该排序规则。
如果尝试使用不兼容的选项或太多的参数创建索引,db.collection.createIndexes()
will return an error if you attempt to create indexes with incompatible options or too many arguments. db.collection.createIndexes()
将返回错误。Refer to the option descriptions for more information.有关更多信息,请参阅选项说明。
The following options are available for all index types unless otherwise specified:除非另有规定,否则以下选项适用于所有索引类型:
background | boolean |
|
unique | boolean |
|
name | string |
|
partialFilterExpression | document |
|
sparse | boolean |
|
expireAfterSeconds | integer | |
hidden | boolean |
|
storageEngine | document |
storageEngine: { <storage-engine-name>: <options> }
|
collation | document |
collation: { locale: <string>, caseLevel: <boolean>, caseFirst: <string>, strength: <int>, numericOrdering: <boolean>, alternate: <string>, maxVariable: <string>, backwards: <boolean> }
|
The following indexes only support simple binary comparison and do not support collation:以下索引仅支持简单的二进制比较,不支持排序规则:
text
2d
geoHaystack
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"} }
。
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
的索引不区分音符和大小写。
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.要使用相同的键模式但不同的排序规则创建索引,必须提供唯一的索引名称。
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
上有一个复合索引;索引是使用排序规则locale"fr"
创建的,用于字符串比较:
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" } )
text
Indexestext
索引选项The following options are available for text indexes only:以下选项仅适用于text
索引:
weights | document |
|
default_language | string | english .english 。
|
language_override | string | text 索引,是集合文档中包含文档重写语言的字段的名称。language . language 。 |
textIndexVersion | integer |
|
2dsphere
Indexes2dsphere
索引选项The following option is available for 2dsphere indexes only:以下选项仅适用于2dsphere
索引:
2dsphereIndexVersion | integer |
|
2d
Indexes2d
索引选项The following options are available for 2d indexes only:以下选项仅适用于2d
索引:
bits | integer |
|
min | number | 2d 索引,经度和纬度值的包含边界下限。-180.0 .-180.0 。
|
max | number | 2d 索引,是经度和纬度值的包含边界上限。180.0 .180.0 。
|
geoHaystack
IndexesgeoHaystack
索引选项The following option is available for geoHaystack indexes only:以下选项仅适用于geoHaystack
索引:
bucketSize | number |
|
MongoDB 5.0 removes the deprecated geoHaystack index and MongoDB 5.0删除了弃用的geoSearch
command. geoHaystack
索引和geoSearch
命令。Use a 2d index with 改为使用带有$geoNear
or one of the supported geospatial query operators instead.$geoNear
或支持的地理空间查询运算符之一的2d
索引。
Upgrading your MongoDB instance to 5.0 and setting featureCompatibilityVersion to 将MongoDB实例升级到5.0并将5.0
will delete any pre-existing geoHaystack indexes.featureCompatibilityVersion
设置为5.0将删除任何预先存在的geoHaystack
索引。
wildcard
indexeswildcard
索引选项The following option is available for wildcard indexes only:以下选项仅适用于wildcard
索引:
wildcardProjection | document |
wildcardProjection: { "path.to.field.a" : <value>, "path.to.field.b" : <value> }
{ "wildcardProjection" : { "_id" : 1, "<field>" : 0|1 } }
|
Changed in version 4.2.在版本4.2中更改。
For featureCompatibilityVersion 对于featureCompatibilityVersion "4.2"
, db.collection.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"
,db.collection.createIndexes()
使用优化的构建过程,在索引构建的开始和结束时获得并保持指定集合的独占锁。All subsequent operations on the collection must wait until 对集合的所有后续操作必须等到db.collection.createIndexes()
releases the exclusive lock. db.collection.createIndexes()
释放独占锁。db.collection.createIndexes()
allows interleaving read and write operations during the majority of the index build.允许在大多数索引构建期间交错读和写操作。
For featureCompatibilityVersion 对于featureCompatibilityVersion "4.0"
, db.collection.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"
,db.collection.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 有关db.collection.createIndexes()
, see Index Builds on Populated Collections.db.collection.createIndexes()
的锁定行为的详细信息,请参阅填充集合上的索引生成。
If you call 如果为已存在的索引调用db.collection.createIndexes()
for an index or indexes that already exist, MongoDB does not recreate the existing index or indexes.db.collection.createIndexes()
,MongoDB不会重新创建现有索引。
With the exception of the collation option, if you create an index with one set of index options and then try to recreate the same index but with different index options, MongoDB will not change the options nor recreate the index.除了排序规则选项之外,如果使用一组索引选项创建索引,然后尝试使用不同的索引选项重新创建相同的索引,MongoDB将不会更改选项,也不会重新创建索引。
The hidden option can be changed without dropping and recreating the index. 可以在不删除和重新创建索引的情况下更改hidden
选项。See Hidden Option.请参见hidden
选项。
To change the other index options, drop the existing index with 要更改其他索引选项,请使用db.collection.dropIndex()
before running db.collection.createIndexes()
with the new options.db.collection.dropIndex()
删除现有索引,然后使用新选项运行db.collection.createIndexes()
。
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.要使用相同的键模式但不同的排序规则创建索引,必须提供唯一的索引名称。
New in version 4.4.在版本4.4中新增。
To hide an index, you must have featureCompatibilityVersion set to 要隐藏索引,必须将4.4
or greater. featureCompatibilityVersion
设置为4.4
或更高。However, once hidden, the index remains hidden even with featureCompatibilityVersion set to 然而,一旦隐藏,即使在MongoDB 4.4二进制文件中将4.2
on MongoDB 4.4 binaries.featureCompatibilityVersion
设置为4.2
,索引仍将保持隐藏。
To hide or unhide existing indexes, you can use the following 要隐藏或取消隐藏现有索引,可以使用以下mongosh
methods: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 } );
For MongoDB 2.6 through MongoDB versions with featureCompatibilityVersion (fCV) set to 对于"4.0"
or earlier, MongoDB will not create an index on a collection if the index entry for an existing document exceeds the Maximum Index Key Length.featureCompatibilityVersion
(fCV)设置为“4.0”或更早版本的MongoDB 2.6到MongoDB版本,如果现有文档的索引条目超过最大索引键长度,MongoDB将不会在集合上创建索引。
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 要在事务中使用db.collection.createIndexes()
in a transaction, the transaction must use read concern "local"
. db.collection.createIndexes()
,事务必须使用读取关注点"local"
。If you specify a read concern level other than 如果指定"local"
, the transaction fails."local"
以外的读取关注级别,则事务将失败。
db.collection.createIndex()
for examples of various index specifications.用于各种索引规范的示例。
Consider a 考虑一个包含类似以下文档的restaurants
collection containing documents that resemble the following:restaurants
集合:
{ location: { type: "Point", coordinates: [-73.856077, 40.848447] }, name: "Morris Park Bake Shop", cuisine: "Cafe", borough: "Bronx", }
The following example creates two indexes on the 下面的示例在restaurants
collection: an ascending index on the borough
field and a 2dsphere index on the location
field.restaurants
集合上创建了两个索引:borough
字段上的升序索引和location
字段上的2dsphere
索引。
db.restaurants.createIndexes([{"borough": 1}, {"location": "2dsphere"}])
The following example creates two indexes on the 下面的示例在products
collection: an ascending index on the manufacturer
field and an ascending index on the category
field. products
集合上创建了两个索引:manufacturer
字段上的升序索引和类别字段上的升序索引。Both indexes use a collation that specifies the locale 这两个索引都使用了指定区域设置fr
and comparison strength 2
:"fr"
和比较强度2
的排序规则:
db.products.createIndexes( [ { "manufacturer": 1}, { "category": 1 } ], { collation: { locale: "fr", strength: 2 } })
For queries or sort operations on the indexed keys that uses the same collation rules, MongoDB can use the index. 对于使用相同排序规则的索引键的查询或排序操作,MongoDB可以使用索引。For details, see Collation and Index Use.有关详细信息,请参阅排序和索引使用。
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部署上设置功能兼容性版本。
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.products_catalog.createIndexes(
[ { "product_attributes.$**" : 1 } ]
)
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.products_catalog.createIndexes(
[ { "$**" : 1 } ]
)
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. To include the _id
field in the wildcard index, you must explicitly include it in the wildcardProjection
document. _id
字段。要在通配符索引中包含_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.products_catalog.createIndexes( [ { "$**" : 1 } ], { "wildcardProjection" : { "product_attributes.elements" : 1, "product_attributes.resistance" : 1 } } )
While the key pattern 虽然键模式"$**"
covers all fields in the document, the wildcardProjection
field limits the index to only the included fields. "$**"
覆盖文档中的所有字段,但wildcardProjection
字段将索引限制为仅包含的字段。For complete documentation on 有关wildcardProjection
, see Options for wildcard
indexes.wildcardProjection
的完整文档,请参阅wildcard
索引的选项。
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_attributeselements
和product_attributesresistance
字段:
use inventory db.products_catalog.createIndexes( [ { "$**" : 1 } ], { "wildcardProjection" : { "product_attributes.elements" : 0, "product_attributes.resistance" : 0 } } )
While the key pattern 虽然键模式"$**"
covers all fields in the document, the wildcardProjection
field excludes the specified fields from the index. "$**"
涵盖了文档中的所有字段,但wildcardProjection
字段将从索引中排除指定字段。For complete documentation on 有关wildcardProjection
, see Options for wildcard
indexes.wildcardProjection
的完整文档,请参阅wildcard
索引的选项。
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 exceptthose 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 parameter 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"
:"majority"
的索引:
db.getSiblingDB("examples").invoices.createIndexes( { "invoices" : 1 }, { }, "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.有关索引生成和投票过程的详细信息,请参阅复制环境中的索引生成。
For additional information about indexes, refer to:有关索引的其他信息,请参阅:
db.collection.getIndexes()
text
索引text
indexes.text
索引的详细信息。