Wildcard Indexes通配符索引
On this page
MongoDB supports creating indexes on a field or set of fields to support queries. MongoDB支持在一个字段或一组字段上创建索引以支持查询。Since MongoDB supports dynamic schemas, applications can query against fields whose names cannot be known in advance or are arbitrary.由于MongoDB支持动态模式,应用程序可以查询那些名称无法预先知道或是任意的字段。
New in version MongoDB: 4.2
MongoDB 4.2 introduces wildcard indexes for supporting queries against unknown or arbitrary fields.MongoDB 4.2引入了通配符索引,用于支持针对未知或任意字段的查询。
Consider an application that captures user-defined data under the 考虑一个应用程序,它在userMetadata
field and supports querying against that data:userMetadata
字段下捕获用户定义的数据,并支持对该数据进行查询:
{ "userMetadata" : { "likes" : [ "dogs", "cats" ] } }
{ "userMetadata" : { "dislikes" : "pickles" } }
{ "userMetadata" : { "age" : 45 } }
{ "userMetadata" : "inactive" }
Administrators want to create indexes to support queries on any subfield of 管理员希望创建索引以支持对userMetadata
.userMetadata
的任何子字段的查询。
A wildcard index on userMetadata
can support single-field queries on userMetadata
, userMetadata.likes
, userMetadata.dislikes
, and userMetadata.age
:userMetadata
上的通配符索引可以支持对userMetadata
、userMetadata.likes
、userMetadata.dislikes
和userMetadata.age
的单字段查询:
db.userData.createIndex( { "userMetadata.$**" : 1 } )
The index can support the following queries:该索引可以支持以下查询:
db.userData.find({ "userMetadata.likes" : "dogs" })
db.userData.find({ "userMetadata.dislikes" : "pickles" })
db.userData.find({ "userMetadata.age" : { $gt : 30 } })
db.userData.find({ "userMetadata" : "inactive" })
A non-wildcard index on userMetadata
can only support queries on values of userMetadata
.userMetadata
上的非通配符索引只能支持对userMetadata
值的查询。
Wildcard indexes are not designed to replace workload-based index planning. 通配符索引不是为了取代基于工作负载的索引规划而设计的。For more information on creating indexes to support queries, see Create Indexes to Support Your Queries. 有关创建索引以支持查询的详细信息,请参阅创建索引以支撑查询。For complete documentation on wildcard index limitations, see Wildcard Index Restrictions.有关通配符索引限制的完整文档,请参阅通配符索引限制。
Create Wildcard Index创建通配符索引
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 6.0 Deployments.有关设置fCV的说明,请参阅在MongoDB 6.0部署上设置功能兼容性版本。
You can create wildcard indexes using the 您可以使用createIndexes
database command or its shell helpers, createIndex()
or createIndexes()
.createIndexes
数据库命令或其shell助手createIndex()
或createIndexes()
创建通配符索引。
Create a Wildcard Index on a Field在字段上创建通配符索引
To index the value of a specific field:要为特定字段的值编制索引,请执行以下操作:
db.collection.createIndex( { "fieldA.$**" : 1 } )
With this wildcard index, MongoDB indexes all values of 通过这个通配符索引,MongoDB为fieldA
. fieldA
的所有值建立索引。If the field is a nested document or array, the wildcard index recurses into the document/array and stores the value for all fields in the document/array.如果字段是嵌套的文档或数组,则通配符索引将递归到文档/数组中,并存储文档/数组中所有字段的值。
For example, documents in the 例如,product_catalog
collection may contain a product_attributes
field. product_catalog
集合中的文档可能包含product_attributes
字段。The product_attributes
field can contain arbitrary nested fields, including embedded documents and arrays:product_attributes
字段可以包含任意嵌套字段,包括嵌入的文档和数组:
{
"product_name" : "Spy Coat",
"product_attributes" : {
"material" : [ "Tweed", "Wool", "Leather" ],
"size" : {
"length" : 72,
"units" : "inches"
}
}
}
{
"product_name" : "Spy Pen",
"product_attributes" : {
"colors" : [ "Blue", "Black" ],
"secret_feature" : {
"name" : "laser",
"power" : "1000",
"units" : "watts",
}
}
}
The following operation creates a wildcard index on the 以下操作在product_attributes
field:product_attributes
字段上创建通配符索引:
db.products_catalog.createIndex( { "product_attributes.$**" : 1 } )
The wildcard index can support arbitrary single-field queries on 通配符索引可以支持对product_attributes
or its embedded fields:product_attributes
或其嵌入字段的任意单字段查询:
db.products_catalog.find( { "product_attributes.size.length" : { $gt : 60 } } )
db.products_catalog.find( { "product_attributes.material" : "Leather" } )
db.products_catalog.find( { "product_attributes.secret_feature.name" : "laser" } )
The path-specific wildcard index syntax is incompatible with the 特定于路径的通配符索引语法与wildcardProjection
option. wildcardProjection
选项不兼容。See the Options for 有关详细信息,请参阅通配符索引的选项。wildcard
indexes for more information.
For an example, see Create a Wildcard Index on a Single Field Path.有关示例,请参阅在单个字段路径上创建通配符索引。
Create a Wildcard Index on All Fields在所有字段上创建通配符索引
To index the value of all fields in a document (excluding 要对文档中所有字段(不包括_id
), specify "$**"
as the index key:_id
)的值进行索引,请指定"$**"
作为索引键:
db.collection.createIndex( { "$**" : 1 } )
With this wildcard index, MongoDB indexes all 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 stores the value for all fields in the document/array.如果给定的字段是嵌套的文档或数组,则通配符索引将递归到文档/数组中,并存储文档/数组中所有字段的值。
For an example, see Create a Wildcard Index on All Field Paths.有关示例,请参阅在所有字段路径上创建通配符索引。
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 Options for 有关详细信息,请参阅通配符索引的选项。wildcard
indexes for more information.
Create a Wildcard Index on Multiple Specific Fields在多个特定字段上创建通配符索引
To index the values of multiple specific fields in a document:要为文档中多个特定字段的值编制索引,请执行以下操作:
db.collection.createIndex(
{ "$**" : 1 },
{ "wildcardProjection" :
{ "fieldA" : 1, "fieldB.fieldC" : 1 }
}
)
With this wildcard index, MongoDB indexes all values for the specified 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 stores the value for all fields in the document/array.如果给定的字段是嵌套的文档或数组,则通配符索引将递归到文档/数组中,并存储文档/数组中所有字段的值。
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 Options for wildcard
indexes.wildcardProjection
的更多信息,请参阅通配符索引的选项。
For an example, see Include Specific Fields in Wildcard Index Coverage.有关示例,请参阅在通配符索引覆盖范围中包含特定字段。
Create a Wildcard Index that Excludes Multiple Specific Fields创建排除多个特定字段的通配符索引
To index the fields of all fields in a document excluding specific field paths:要为文档中除特定字段路径之外的所有字段的字段编制索引,请执行以下操作:
db.collection.createIndex(
{ "$**" : 1 },
{ "wildcardProjection" :
{ "fieldA" : 0, "fieldB.fieldC" : 0 }
}
)
With this wildcard index, MongoDB indexes all fields for each document in the collection excluding the specified field paths. 有了这个通配符索引,MongoDB为集合中每个文档的所有字段建立索引,不包括指定的字段路径。If a given field is a nested document or array, the wildcard index recurses into the document/array and stores the values for all fields in the document/array.如果给定的字段是嵌套的文档或数组,则通配符索引将递归到文档/数组中,并存储文档/数组中所有字段的值。
For an example, see Omit Specific Fields from Wildcard Index Coverage.有关示例,请参阅从通配符索引覆盖范围中省略特定字段。
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 Options for wildcard
indexes.wildcardProjection
的更多信息,请参阅通配符索引的选项。
Considerations注意事项
-
Wildcard indexes can support at most one field in any given query predicate.通配符索引在任何给定的查询谓词中最多可以支持一个字段。For more information on wildcard index query support, see Wildcard Index Query/Sort Support.有关通配符索引查询支持的更多信息,请参阅通配符索引查询/排序支持。 -
Themongod
featureCompatibilityVersion must be4.2
to create wildcard indexes.mongod
featureCompatibilityVersion
必须为4.2才能创建通配符索引。For instructions on setting the fCV, see Set Feature Compatibility Version on MongoDB 6.0 Deployments.有关设置fCV的说明,请参阅在MongoDB 6.0部署上设置功能兼容性版本。 -
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 (i.e.{ "_id" : 1 }
)._id
字段,必须在wildcardProjection
文档中显式包含它(即{ "_id" : 1 }
)。 -
You can create multiple wildcard indexes in a collection.可以在集合中创建多个通配符索引。 -
A wildcard index may cover the same fields as other indexes in the collection.通配符索引可以覆盖与集合中其他索引相同的字段。 -
Wildcard indexes are Sparse Indexes and only contain entries for documents that have the indexed field, even if the index field contains a null value.通配符索引是稀疏索引,仅包含具有索引字段的文档的条目,即使索引字段包含空值也是如此。
Behavior行为
Wildcard indexes have specific behavior when indexing fields which are an object (i.e. an embedded document) or an array:通配符索引在索引对象(即嵌入文档)或数组字段时具有特定行为:
-
If the field is an object, the wildcard index descends into the object and indexes its contents.如果字段是一个对象,则通配符索引会下降到对象中并对其内容进行索引。The wildcard index continues descending into any additional embedded documents it encounters.通配符索引继续下降到它遇到的任何其他嵌入文档中。 -
If the field is an array, then the wildcard index traverses the array and indexes each element:如果字段是数组,则通配符索引遍历数组并对每个元素进行索引:-
If an element in the array is an object, the wildcard index descends into the object to index its contents as described above.如果数组中的元素是一个对象,则通配符索引会下降到该对象中,以对其内容进行索引,如上所述。 -
If the element is an array - that is, an array which is embedded directly within the parent array - then the wildcard index does not traverse the embedded array, but indexes the entire array as a single value.如果元素是一个数组,即直接嵌入父数组中的数组,则通配符索引不会遍历嵌入的数组,而是将整个数组作为一个值进行索引。
-
-
For all other fields, record the primitive (non-object/array) value into the index.对于所有其他字段,将基元(非对象/数组)值记录到索引中。
The wildcard index continues traversing any additional nested objects or arrays until it reaches a primitive value (i.e. a field that is not an object or array). 通配符索引继续遍历任何其他嵌套对象或数组,直到它达到基元值(即不是对象或数组的字段)。It then indexes this primitive value, along with the full path to that field.然后,它对该基元值以及该字段的完整路径进行索引。
For example, consider the following document:例如,考虑以下文档:
{
"parentField" : {
"nestedField" : "nestedValue",
"nestedObject" : {
"deeplyNestedField" : "deeplyNestedValue"
},
"nestedArray" : [
"nestedArrayElementOne",
[ "nestedArrayElementTwo" ]
]
}
}
A wildcard index which includes 包含parentField的通配符索引记录以下条目:parentField
records the following entries:
-
"parentField.nestedField" : "nestedValue"
-
"parentField.nestedObject.deeplyNestedField" : "deeplyNestedValue"
-
"parentField.nestedArray" : "nestedArrayElementOne"
-
"parentField.nestedArray" : ["nestedArrayElementTwo"]
Note that the records for 请注意,parentField.nestedArray
do not include the array position for each element. parentField.nestedArray
的记录不包括每个元素的数组位置。Wildcard indexes ignore array element positions when recording the element into the index. 将数组元素记录到索引中时,通配符索引将忽略数组元素的位置。Wildcard indexes can still support queries that include explicit array indices. 通配符索引仍然可以支持包含显式数组索引的查询。See Queries with Explicit Array Indices for more information.有关详细信息,请参阅具有显式数组索引的查询。
For more information on wildcard index behavior with nested objects, see Nested Objects.有关嵌套对象的通配符索引行为的详细信息,请参阅嵌套对象。
For more information on wildcard index behavior with nested arrays, see Nested Arrays.有关嵌套数组的通配符索引行为的详细信息,请参阅嵌套数组。
Nested Objects嵌套对象
When a wildcard index encounters a nested object, it descends into the object and indexes its contents. 当通配符索引遇到嵌套对象时,它会下降到对象中并对其内容进行索引。For example:例如:
{
"parentField" : {
"nestedField" : "nestedValue",
"nestedArray" : ["nestedElement"]
"nestedObject" : {
"deeplyNestedField" : "deeplyNestedValue"
}
}
}
A wildcard index which includes 包含parentField
descends into the object to traverse and index its contents:parentField
的通配符索引下降到对象中以遍历和索引其内容:
-
For each field which is itself an object (i.e. an embedded document), descend into the object to index its contents.对于每个本身就是对象(即嵌入式文档)的字段,向下进入对象以索引其内容。 -
For each field which is an array, traverse the array and index its contents.对于作为数组的每个字段,遍历数组并索引其内容。 -
For all other fields, record the primitive (non-object/array) value into the index.对于所有其他字段,将基元(非对象/数组)值记录到索引中。
The wildcard index continues traversing any additional nested objects or arrays until it reaches a primitive value (i.e. a field that is not an object or array). 通配符索引继续遍历任何其他嵌套对象或数组,直到它达到基元值(即不是对象或数组的字段)。It then indexes this primitive value, along with the full path to that field.然后,它对该基元值以及该字段的完整路径进行索引。
Given the sample document, the wildcard index adds the following records to the index:给定示例文档,通配符索引将以下记录添加到索引中:
-
"parentField.nestedField" : "nestedValue"
-
"parentField.nestedObject.deeplyNestedField" : "deeplyNestedValue"
-
"parentField.nestedArray" : "nestedElement"
For more information on wildcard index behavior with nested arrays, see Nested Arrays.有关嵌套数组的通配符索引行为的详细信息,请参阅嵌套数组。
Nested Arrays嵌套数组
When a wildcard index encounters a nested array, it attempts to traverse the array to index its elements. 当通配符索引遇到嵌套数组时,它会尝试遍历数组以对其元素进行索引。If the array is itself an element in a parent array (i.e. an embedded array), the wildcard index instead records the entire array as a value instead of traversing its contents. 如果数组本身是父数组(即嵌入式数组)中的一个元素,则通配符索引会将整个数组记录为一个值,而不是遍历其内容。For example:例如:
{
"parentArray" : [
"arrayElementOne",
[ "embeddedArrayElement" ],
"nestedObject" : {
"nestedArray" : [
"nestedArrayElementOne",
"nestedArrayElementTwo"
]
}
]
}
A wildcard index which includes 包含parentArray
descends into the array to traverse and index its contents:parentArray
的通配符索引下降到数组中,以遍历和索引其内容:
-
For each element which is an array (i.e. an embedded array), index the entire array as a value.对于作为数组的每个元素(即嵌入式数组),将整个数组作为一个值进行索引。 -
For each element which is an object, descend into the object to traverse and index its contents.对于作为对象的每个元素,向下进入对象以遍历和索引其内容。 -
For all other fields, record the primitive (non-object/array) value into the index.对于所有其他字段,将基元(非对象/数组)值记录到索引中。
The wildcard index continues traversing any additional nested objects or arrays until it reaches a primitive value (i.e. a field that is not an object or array). 通配符索引继续遍历任何其他嵌套对象或数组,直到它达到基元值(即不是对象或数组的字段)。It then indexes this primitive value, along with the full path to that field.然后,它对该基元值以及该字段的完整路径进行索引。
Given the sample document, the wildcard index adds the following records to the index:给定示例文档,通配符索引将以下记录添加到索引中:
-
"parentArray" : "arrayElementOne"
-
"parentArray" : ["embeddedArrayElement"]
-
"parentArray.nestedObject.nestedArray" : "nestedArrayElementOne"
-
"parentArray.nestedObject.nestedArray" : "nestedArrayElementTwo"
Note that the records for 请注意,parentField.nestedArray
do not include the array position for each element. parentField.nestedArray
的记录不包括每个元素的数组位置。Wildcard indexes ignore array element positions when recording the element into the index. Wildcard indexes can still support queries that include explicit array indices. 将数组元素记录到索引中时,通配符索引将忽略数组元素的位置。通配符索引仍然可以支持包含显式数组索引的查询。See Queries with Explicit Array Indices for more information.有关详细信息,请参阅具有显式数组索引的查询。
See also: 另请参阅:
Index Display索引显示
Starting in MongoDB 6.3, 6.0.5, and 5.0.16, the 从MongoDB 6.3、6.0.5和5.0.16开始,wildcardProjection
field stores the index projection in its submitted form. Earlier versions of the server may have stored the projection in a normalized form.wildcardProjection
字段以提交的形式存储索引投影。早期版本的服务器可能以规范化的形式存储了投影。
The server uses the index the same way, but you may notice a difference in the output of the 服务器以相同的方式使用索引,但您可能会注意到listIndexes
and db.collection.getIndexes()
commands.listIndexes
和db.collection.getIndexes()
命令的输出有所不同。
Restrictions限制
-
You cannot shard a collection using a wildcard index.不能使用通配符索引对集合进行分片。Create a non-wildcard index on the field or fields you want to shard on.在要对其进行分片的一个或多个字段上创建一个非通配符索引。For more information on shard key selection, see Shard Keys.有关分片关键帧选择的详细信息,请参阅分片关键帧。 -
You cannot specify the following properties for a wildcard index:不能为通配符索引指定以下属性: -
You cannot create the following index types using wildcard syntax:不能使用通配符语法创建以下索引类型:
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 creation restrictions, see Incompatible Index Types or Properties.有关通配符索引创建限制的完整文档,请参阅不兼容的索引类型或属性。
Wildcard Index Query/Sort Support通配符索引查询/排序支持
Covered Queries涵盖的查询
Wildcard indexes can support a covered query only if all of the following are true:只有当以下所有条件都为true
时,通配符索引才能支持覆盖查询:
-
The query planner selects the wildcard index for satisfying the query predicate.查询计划器选择通配符索引以满足查询谓词。 -
The query predicate specifies exactly one field covered by the wildcard index.查询谓词只指定通配符索引所覆盖的一个字段。 -
The projection explicitly excludes投影显式地排除了_id
and includes only the query field._id
,并且只包括查询字段。 -
The specified query field is never an array.指定的查询字段从来都不是数组。
Consider the following wildcard index on the 请考虑对employees
collection:employees
集合使用以下通配符索引:
db.employees.createIndex( { "$**" : 1 } )
The following operation queries for a single field 以下操作查询单个字段lastName
and projects out all other fields from the resulting document:lastName
,并从生成的文档中投射出所有其他字段:
db.employees.find(
{ "lastName" : "Doe" },
{ "_id" : 0, "lastName" : 1 }
)
Assuming that the specified 假设指定的lastName
is never an array, MongoDB can use the $**
wildcard index for supporting a covered query.lastName
从来都不是数组,MongoDB可以使用$**
通配符索引来支持覆盖的查询。
Multi-Field Query Predicates多字段查询谓词
Wildcard indexes can support at most one query predicate field. That is:通配符索引最多可以支持一个查询谓词字段。即:
-
MongoDB cannot use a non-wildcard index to satisfy one part of a query predicate and a wildcard index to satisfy another.MongoDB不能使用非通配符索引来满足查询谓词的一部分,也不能使用通配符索引来实现另一部分。 -
MongoDB cannot use one wildcard index to satisfy one part of a query predicate and another wildcard index to satisfy another.MongoDB不能使用一个通配符索引来满足查询谓词的一部分,而不能使用另一个通配符指数来满足另一部分。 -
Even if a single wildcard index could support multiple query fields, MongoDB can use the wildcard index to support only one of the query fields. All remaining fields are resolved without an index.即使单个通配符索引可以支持多个查询字段,MongoDB也可以使用通配符索引只支持其中一个查询字段。解析所有剩余字段时不使用索引。
However, MongoDB may use the same wildcard index for satisfying each independent argument of the query 然而,MongoDB可能使用相同的通配符索引来满足查询$or
or aggregation $or
operators.$or
或聚合$or
运算符的每个独立参数。
Queries with Sort带排序的查询
MongoDB can use a wildcard index for satisfying the 只有当以下所有条件都为sort()
only if all of the following are true:true
时,MongoDB才能使用通配符索引来满足sort()
:
-
The query planner selects the wildcard index for satisfying the query predicate.查询计划器选择通配符索引以满足查询谓词。 -
Thesort()
specifies only the query predicate field.sort()
只指定查询谓词字段。 -
The specified field is never an array.指定的字段从来都不是数组。
If the above conditions are not met, MongoDB cannot use the wildcard index for the sort. 如果不满足上述条件,MongoDB将无法使用通配符索引进行排序。MongoDB does not support MongoDB不支持sort()
operations that require a different index from that of the query predicate. sort()
操作,这些操作需要与查询谓词不同的索引。For more information, see Index Intersection and Sort.有关详细信息,请参阅索引交点和排序。
Consider the following wildcard index on the 请考虑products
collection:products
集合上的以下通配符索引:
db.products.createIndex( { "product_attributes.$**" : 1 } )
The following operation queries for a single field 以下操作查询单个字段product_attributes.price
and sorts on that same field:product_attributes.price
并对同一字段进行排序:
db.products.find(
{ "product_attributes.price" : { $gt : 10.00 } },
).sort(
{ "product_attributes.price" : 1 }
)
Assuming that the specified 假设指定的price
is never an array, MongoDB can use the product_attributes.$**
wildcard index for satisfying both the find()
and sort()
.price
从来都不是数组,MongoDB可以使用product_attributes.$**
通配符索引,用于同时满足find()
和sort()
。
Unsupported Query Patterns不支持的查询模式
Wildcard indexes cannot support the following query patterns:通配符索引无法支持以下查询模式:
-
Queries that check if a field does not exist检查字段是否不存在的查询 -
Queries that check if a field is or is not equal to a document or an array检查字段是否等于文档或数组的查询 -
Queries that check if a field is equal to null检查字段是否等于null
的查询
For details, see Unsupported Query and Aggregation Patterns.有关详细信息,请参阅不支持的查询和聚合模式。
Queries with Explicit Array Indices具有显式数组索引的查询
MongoDB wildcard indexes do not record the array position of any given element in an array during indexing. However, MongoDB may still select the wildcard index to answer a query which includes a field path with one or more explicit array indices (for example, MongoDB通配符索引在索引过程中不会记录数组中任何给定元素的数组位置。但是,MongoDB仍然可以选择通配符索引来回答包含一个或多个显式数组索引(例如parentArray.0.nestedArray.0
). parentArray.0.nestedArray.0
)的字段路径的查询。Due to the increasing complexity of defining index bounds for each consecutive nested array, MongoDB does not consider the wildcard index to answer a given field path in the query if that path contains more than 由于为每个连续嵌套数组定义索引边界的复杂性不断增加,如果查询中的给定字段路径包含8
explicit array indices. 8
个以上的显式数组索引,MongoDB不会考虑使用通配符索引来回答该路径。MongoDB can still consider the wildcard index to answer other field paths in the query.MongoDB仍然可以考虑通配符索引来回答查询中的其他字段路径。
For example:例如:
{
"parentObject" : {
"nestedArray" : [
"elementOne",
{
"deeplyNestedArray" : [ "elementTwo" ]
}
]
}
}
MongoDB can select a wildcard index which includes MongoDB可以选择一个包含parentObject
to satisfy the following queries:parentObject
的通配符索引来满足以下查询:
-
"parentObject.nestedArray.0" : "elementOne"
-
"parentObject.nestedArray.1.deeplyNestedArray.0" : "elementTwo"
If a given field path in the query predicate specifies more than 8 explicit array indices, MongoDB does not consider the wildcard index for answering that field path. 如果查询谓词中给定的字段路径指定了8个以上的显式数组索引,MongoDB不会考虑通配符索引来回答该字段路径。MongoDB instead either selects another eligible index to answer the query, or performs a collection scan.相反,MongoDB要么选择另一个符合条件的索引来回答查询,要么执行集合扫描。
Note that wildcard indexes themselves do not have any limits on the depth to which they traverse a document while indexing it; the limitation only applies to queries which explicitly specify exact array indices. 请注意,通配符索引本身对索引文档时遍历文档的深度没有任何限制;该限制仅适用于明确指定精确数组索引的查询。By issuing the same queries without the explicit array indices, MongoDB may select the wildcard index to answer the query:通过在没有显式数组索引的情况下发出相同的查询,MongoDB可以选择通配符索引来回答查询:
-
"parentObject.nestedArray" : "elementOne"
-
"parentObject.nestedArray.deeplyNestedArray" : "elementTwo"