Docs Home → Develop Applications → MongoDB Manual
Wildcard Indexes通配符索引
On this page本页内容
MongoDB supports creating indexes on a field, or set of fields, to improve performance for queries. MongoDB支持在一个字段或一组字段上创建索引,以提高查询性能。MongoDB supports flexible schemas, meaning document field names may differ within a collection. MongoDB支持灵活的架构,这意味着文档字段名称在一个集合中可能有所不同。Use wildcard indexes to support queries against arbitrary or unknown fields.使用通配符索引支持针对任意或未知字段的查询。
To create a wildcard index, use the wildcard specifier (要创建通配符索引,请使用通配符说明符($**
) as the index key:$**
)作为索引键:
db.collection.createIndex( { "$**": <sortOrder> } )
You can use the following commands to create a wildcard index:可以使用以下命令创建通配符索引:
Use Cases用例
Only use wildcard indexes when the fields you want to index are unknown or may change. 只有当要索引的字段未知或可能更改时,才使用通配符索引。Wildcard indexes don't perform as well as targeted indexes on specific fields. 通配符索引在特定字段上的性能不如目标索引。If your collection contains arbitrary field names that prevent targeted indexes, consider remodeling your schema to have consistent field names. 如果您的集合包含阻止目标索引的任意字段名,请考虑重新构建架构,使其具有一致的字段名。To learn more about targeted indexes, see Create Indexes to Support Your Queries.要了解有关目标索引的更多信息,请参阅创建索引以支持查询。
Consider using a wildcard index in the following scenarios:在以下情况下,请考虑使用通配符索引:
If your application queries a collection where field names vary between documents, create a wildcard index to support queries on all possible document field names.如果应用程序查询的集合中的字段名称因文档而异,请创建通配符索引以支持对所有可能的文档字段名称的查询。If your application repeatedly queries an embedded document field where the subfields are not consistent, create a wildcard index to support queries on all of the subfields.如果应用程序重复查询子字段不一致的嵌入文档字段,请创建通配符索引以支持对所有子字段的查询。If your application queries documents that share common characteristics.如果您的应用程序查询具有共同特征的文档。A compound wildcard index can efficiently cover many queries for documents that have common fields.复合通配符索引可以有效地覆盖对具有公共字段的文档的许多查询。To learn more, see Compound Wildcard Indexes.要了解更多信息,请参阅复合通配符索引。
Get Started起步
You can perform the following tasks with wildcard indexes:您可以使用通配符索引执行以下任务:
Create a Wildcard Index on a Single Field在单个字段上创建通配符索引Include or Exclude Fields in a Wildcard Index包含或排除通配符索引中的字段Create a Wildcard Index on All Fields在所有字段上创建通配符索引Create a Compound Wildcard Index创建复合通配符索引
Details详细信息
Wildcard indexes behave as follows:通配符索引的行为如下:
You can create multiple wildcard indexes in a collection.可以在集合中创建多个通配符索引。A wildcard index can cover the same fields as other indexes in the collection.通配符索引可以覆盖与集合中其他索引相同的字段。Wildcard indexes omit the默认情况下,通配符索引会省略_id
field by default._id
字段。To include the要在通配符索引中包含_id
field in the wildcard index, you must explicitly include it in thewildcardProjection
document by specifying{ "_id" : 1 }
._id
字段,必须通过指定{ "_id" : 1 }
将其显式包含在wildcardProjection
文档中。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.通配符索引是稀疏索引,仅包含具有索引字段的文档的条目,即使索引字段包含空值也是如此。Wildcard indexes are distinct from and incompatible with wildcard text indexes.通配符索引不同于通配符文本索引,并且与通配符文本索引不兼容。Wildcard indexes cannot support queries using the通配符索引不能支持使用$text
operator.$text
运算符的查询。
Covered Queries涵盖的查询
Wildcard indexes can support a covered query only if all of the following conditions are true:只有在满足以下所有条件的情况下,通配符索引才能支持覆盖查询:
The query planner selects the wildcard index to fulfill the query predicate.查询计划器选择通配符索引来完成查询谓词。The query predicate specifies exactly one field covered by the wildcard index.查询谓词只指定通配符索引所覆盖的一个字段。The query 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 }
)
If the specified 如果指定的lastName
is never an array, MongoDB can use the $**
wildcard index to support a covered query.lastName
从来都不是数组,MongoDB可以使用$**
通配符索引来支持覆盖查询。
Learn More了解更多信息
To learn more about wildcard indexes, see:要了解有关通配符索引的更多信息,请参阅: