On this page本页内容
db.collection.createIndex(keys, 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 indexes on collections.在集合上创建索引。
db.collection.createIndex()
takes the following parameters:采用以下参数:
keys | document |
|
options | document | |
commitQuorum | integer or string |
|
The db.collection.createIndex()
is a wrapper around the createIndexes
command.db.collection.createIndex()
是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 controls the creation of the index. 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.createIndex()
operation will fail.db.collection.createIndex()
操作将失败。
Consider the following 考虑以下db.collection.createIndex()
operation:db.collection.createIndex()
操作:
db.collection.createIndex( { "a": 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 }
,索引创建操作将失败。
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 is not supported in MongoDB 3.2 and earlier versions. MongoDB 3.2和更早版本不支持排序规则。In MongoDB 3.2 and earlier versions, do not create indexes with the unsupported collation option as this will block the upgrade to 3.4, which enforces a stricter validation of index options.在MongoDB 3.2和更早版本中,不要使用不支持的排序选项创建索引,因为这会阻止升级到3.4,这会强制对索引选项进行更严格的验证。
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:以下索引仅支持简单的二进制比较,不支持排序规则:
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:但是,以下查询操作(默认情况下使用“简单”二进制collator)无法使用索引:
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
上有一个复合索引;索引是使用排序规则语言环境"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 | text 索引,确定停止词列表以及词干分析器和标记器规则的语言。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:以下选项仅适用于通配符索引:
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 对于"4.2"
, db.collection.createIndex()
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. featureCompatibilityVersion
为"4.2"
,db.collection.createIndex()
使用优化的构建过程,该过程在索引构建的开始和结束时获取并保持指定集合的独占锁。All subsequent operations on the collection must wait until 集合上的所有后续操作都必须等待db.collection.createIndex()
releases the exclusive lock. db.collection.createIndex()
释放独占锁。db.collection.createIndex()
allows interleaving read and write operations during the majority of the index build.允许在大多数索引构建期间交叉执行读写操作。
For featureCompatibilityVersion 对于"4.0"
, db.collection.createIndex()
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. featureCompatibilityVersion
"4.0"
,db.collection.createIndex()
使用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.background
索引不采用独占锁。
For more information on the locking behavior of 有关db.collection.createIndex()
, see Index Builds on Populated Collections.db.collection.createIndex()
锁定行为的更多信息,请参阅在填充集合上构建索引。
If you call 如果为已经存在的索引调用db.collection.createIndex()
for an index that already exists, MongoDB does not recreate the index.db.collection.createIndex()
,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. 可以在不删除和重新创建索引的情况下更改隐藏选项。See Hidden Option.请参见隐藏选项。
To change the other index options, drop the existing index with 要更改其他索引选项,请先使用db.collection.dropIndex()
before running db.collection.createIndex()
with the new options.db.collection.dropIndex()
删除现有索引,然后使用新选项运行db.collection.createIndex()
。
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.featureCompatibilityVersio
n设置为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将不会在集合上创建索引。
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.createIndex()
in a transaction, the transaction must use read concern "local"
. db.collection.createIndex()
,事务必须使用读取关注点"local"
。If you specify a read concern level other than 如果指定的读取关注级别不是"local"
, the transaction fails."local"
,则事务将失败。
The following example creates an ascending index on the field 以下示例在字段orderDate
.orderDate
上创建升序索引。
db.collection.createIndex( { orderDate: 1 } )
If the 如果keys
document specifies more than one field, then createIndex()
creates a compound index.keys
文档指定了多个字段,那么createIndex()
将创建一个复合索引。
The following example creates a compound index on the 下面的示例在orderDate
field (in ascending order) and the zipcode
field (in descending order.)orderDate
字段(升序)和zipcode
字段(降序)上创建一个复合索引
db.collection.createIndex( { orderDate: 1, zipcode: -1 } )
Changed in version 4.4.在版本4.4中更改。
4.4
.featureCompatibilityVersion
设置为4.4
。
The following example creates a compound index on the 以下示例在state
field (in ascending order) and the zipcode
field (hashed):state
字段(升序)和zipcode
字段(散列)上创建复合索引:
db.collection.createIndex( { "state" : 1, "zipcode" : "hashed" } )
The order of fields in a compound index is important for supporting 复合索引中字段的顺序对于支持使用索引的sort()
operations using the index.sort()
操作很重要。
The following example creates an index named 以下示例创建一个名为category_fr
. category_fr
的索引。The example creates the index with the collation that specifies the locale 该示例使用指定语言环境fr
and comparison strength 2
:fr
和比较强度2
的排序规则创建索引:
db.collection.createIndex( { category: 1 }, { name: "category_fr", collation: { locale: "fr", strength: 2 } } )
The following example creates a compound index named 以下示例使用排序规则创建名为date_category_fr
with a collation. date_category_fr
的复合索引。The collation applies only to the index keys with string values.排序规则仅适用于具有字符串值的索引键。
db.collection.createIndex( { orderDate: 1, category: 1 }, { name: "date_category_fr", collation: { locale: "fr", strength: 2 } } )
The collation applies to the indexed keys whose values are string.排序规则适用于值为字符串的索引键。
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部署上设置功能兼容性版本。
_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
文档中组合包含和排除语句。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 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.createIndex( { "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.createIndex( { "$**" : 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. _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.products_catalog.createIndex( { "$**" : 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_attributes.elements
和product_attributes.resistance
字段:
use inventory db.products_catalog.createIndex( { "$**" : 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
的详细信息,请参阅参数文档。
featureCompatibilityVersion
4.4+
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. See Index Builds in Replicated Environments for more information.有关详细信息,请参阅复制环境中的索引构建。
Specify the commitQuorum parameter to the 为createIndex()
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. createIndex()
操作指定commitQuorum
参数,以设置包含投票成员(即commit quorum)的数据的最小数量,包括主要成员,这些成员必须在主要成员将索引标记为就绪之前完成索引构建。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").invoices.createIndex( { "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.有关索引生成和投票过程的详细信息,请参阅复制环境中的索引生成。
db.collection.getIndexes()
text
indexes.text
索引,了解有关创建text
索引的详细信息。