Docs HomeDevelop ApplicationsMongoDB Manual

Wildcard Indexes通配符索引

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:您可以使用通配符索引执行以下任务:

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 the wildcardProjection 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:要了解有关通配符索引的更多信息,请参阅: