Docs Menu
Docs Home
/
MongoDB Manual
/ /

Wildcard Indexes

On this page

  • Use Cases
  • Get Started
  • Details
  • Covered Queries
  • Learn More

MongoDB supports creating indexes on a field, or set of fields, to improve performance for queries. MongoDB supports flexible schemas, meaning document field names may differ within a collection. 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:

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.

You can perform the following tasks with wildcard indexes:

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. To include the _id field in the wildcard index, you must explicitly include it in the wildcardProjection document by specifying { "_id" : 1 }.

  • 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.

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.

  • The specified query field is never an array.

Consider the following wildcard index on the employees collection:

db.employees.createIndex( { "$**" : 1 } )

The following operation queries for a single field lastName and projects out all other fields from the resulting document:

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.

To learn more about wildcard indexes, see:

Back

Bounds