Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Wildcard Index Restrictions

On this page

  • Compound Wildcard Index Restrictions
  • Incompatible Index Properties
  • Incompatible Index Types
  • Shard Key
  • Unsupported Query Patterns
  • Array Field is Not Equal to null
  • Equality Matches on Documents and Arrays
  • Field Does Not Exist
  • Multi-Field Query Predicates
  • Queries with Sort

This page describes limitations for wildcard indexes such as incompatible properties and unsupported query patterns.

Compound wildcard indexes have the following restrictions:

  • A compound wildcard index can only have one wildcard term.

    For example, you cannot specify the following index:

    { userID: 1, "object1.$**": 1, "object2.$**": 1 }
  • The non-wildcard terms in a compound wildcard index must be single key terms. Multikey index terms are not permitted.

  • The wildcardProjection option is only valid when the wildcard field is $**. You cannot use wildcardProjection when you specify a field path for the wildcard index term.

    This is a valid definition:

    {
    key: { "$**": 1 },
    name: "index_all_with_projection",
    wildcardProjection: {
    "someFields.name": 1,
    "otherFields.values": 1
    }
    }

    This is an invalid definition:

    {
    key: { "someFields.$**": 1 },
    name: "invalid_index",
    wildcardProjection: {
    "someFields.name": 1,
    "otherFields.values": 1
    }
    }
  • The _id field is omitted by default. If you need the _id field:

    • Specify a wildcard index as $**

    • Use a wildcardProjection

    • Specify the _id field

    db.studentGrades.createIndex(
    {
    "$**": 1,
    },
    {
    wildcardProjection: {
    _id: 1,
    exams: 1,
    extraCredit: 1
    }
    }
    )
  • You cannot include the same field in the wildcard fields and the regular fields. You can use a wildcardProjection to exclude fields from the wildcard pattern.

    db.studentGrades.createIndex(
    {
    exams: 1,
    "$**": 1,
    homeworks: 1
    },
    {
    wildcardProjection: {
    exams: 0,
    homeworks: 0
    }
    }
    )

You cannot specify the following properties for a wildcard index:

You cannot create the following index types using wildcard syntax ($.**):

Note

Disambiguation

Wildcard Indexes are distinct from and incompatible with Create a Wildcard Text Index. Wildcard indexes cannot support queries using the $text operator.

You cannot use a wildcard index as a shard key index.

Wildcard indexes cannot support the following query patterns:

If a given field is an array in any document in the collection, wildcard indexes cannot support queries for documents where that field is not equal to null.

For example, consider an inventory collection with a wildcard index on product_attributes. The wildcard index cannot support the following queries if product_attributes.tags is an array in any document in the collection:

db.inventory.find( { $ne : [ "product_attributes.tags", null ] } )
db.inventory.aggregate( [
{
$match : { $ne : [ "product_attributes.tags", null ] }
}
] )

Wildcard indexes store entries for the contents of a document or array, not the document or array itself. Therefore, wildcard indexes cannot support exact equality matches on documents or arrays.

For example, consider an inventory collection with a wildcard index on product_attributes. The wildcard index cannot support the following queries:

db.inventory.find(
{
"product_attributes" : { "price" : 29.99 }
}
)
db.inventory.find(
{
"product_attributes.tags" : [ "waterproof", "fireproof" ]
}
)

Note

Wildcard indexes can support queries where the field equals an empty document {}.

Similarly, wildcard indexes cannot support exact inequality matches on documents and arrays. For example, a wildcard index on product_attributes cannot support the following queries:

db.inventory.aggregate( [
{
$match : {
$ne : [ "product_attributes", { "price" : 29.99 } ]
}
}
] )
db.inventory.aggregate( [
{
$match : {
$ne : [ "product_attributes.tags", [ "waterproof", "fireproof" ] ]
}
}
] )

Wildcard indexes are sparse and do not index empty fields. Therefore, wildcard indexes cannot support queries for documents where a field does not exist.

For example, consider an inventory collection with a wildcard index on product_attributes. The wildcard index cannot support the following queries:

db.inventory.find(
{
"product_attributes" : { $exists : false }
}
)
db.inventory.aggregate( [
{
$match : {
"product_attributes" : { $exists : false }
}
}
] )

Wildcard indexes can support at most one query predicate field. This means that:

  • MongoDB can't use a non-wildcard index to support one part of a query predicate and a wildcard index to support another.

  • MongoDB can't use multiple wildcard indexes to support different predicates in the same query.

  • In the case that a single wildcard index could support multiple query fields, MongoDB can only use the wildcard index to support one of the query fields. MongoDB chooses which field to support with the wildcard index automatically based on relevant wildcard index paths.

For example, consider an inventory collection with a wildcard index on product_attributes. The wildcard index cannot support all of the predicates in the following query:

db.inventory.find(
{
"product_attributes.price": { $gt: 20 },
"product_attributes.material": "silk",
"product_attributes.size": "large"
}
)

Instead, MongoDB uses the wildcard index to support only one of the query predicates. MongoDB chooses which predicate to support based on relevant wildcard index paths. The unsupported query predicates are shown in the rejectedPlans of the explain results.

Note

$or Behavior

MongoDB may use the same wildcard index to support each independent argument of the query $or or aggregation $or operators.

MongoDB can use a wildcard index for satisfying the sort() only if all of the following are true:

  • The query planner selects the wildcard index for satisfying the query predicate.

  • The sort() specifies only the query predicate field.

  • The specified field is never an array.

If the above conditions are not met, MongoDB cannot use the wildcard index for the sort. MongoDB does not support sort() operations that require a different index from that of the query predicate.

Consider the following wildcard index on the products collection:

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

The following operation queries for a single field product_attributes.price and sorts on that same field:

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().

← Wildcard Index Signature