Wildcard Index Restrictions

Incompatible Index Types or Properties

Wildcard indexes do not support the following index types orproperties:

Note

Wildcard Indexes are distinct from and incompatible withWildcard Text Indexes. Wildcard indexes cannot supportqueries using the $text operator.

Unsupported Query and Aggregation Patterns

  • Field does not exist
  • Wildcard indexes are sparse and donot index empty fields. Wildcard indexes therefore cannot supportquerying for documents where a field does not exist.

For example, consider a collection inventory with a wildcardindex on product_attributes. The wildcard indexcannot support the following queries:

  1. db.inventory.find( {"product_attributes" : { $exists : false } } )
  2.  
  3. db.inventory.aggregate([
  4. { $match : { "product_attributes" : { $exists : false } } }
  5. ])
  • Field is equal to a document or an array
  • Wildcard indexes generate entries for the contents of adocument or array, and not the document/array itself. Wildcardindexes therefore cannot support exact document/array equalitymatches. Wildcard indexes can support querying wherethe field equals an empty document {}.

For example, consider a collection inventory with a wildcardindex on product_attributes. The wildcard indexcannot support the following queries:

  1. db.inventory.find({ "product_attributes" : { "price" : 29.99 } } )
  2. db.inventory.find({ "product_attributes.tags" : [ "waterproof", "fireproof" ] } )
  3.  
  4. db.inventory.aggregate([{
  5. $match : { "product_attributes" : { "price" : 29.99 } }
  6. }])
  7.  
  8. db.inventory.aggregate([{
  9. $match : { "product_attributes.tags" : ["waterproof", "fireproof" ] } }
  10. }])
  • Field is not equal to a document or array
  • Wildcard indexes generate entries for the contents of adocument or array, and not the document/array itself. Wildcardindexes therefore cannot support exact document/array inequalitymatches.

For example, consider a collection inventory with a wildcardindex on product_attributes. The wildcard indexcannot support the following queries:

  1. db.inventory.find( { $ne : [ "product_attributes", { "price" : 29.99 } ] } )
  2. db.inventory.find( { $ne : [ "product_attributes.tags", [ "waterproof", "fireproof" ] ] } )
  3.  
  4. db.inventory.aggregate([{
  5. $match : { $ne : [ "product_attributes", { "price" : 29.99 } ] }
  6. }])
  7.  
  8. db.inventory.aggregate([{
  9. $match : { $ne : [ "product_attributes.tags", [ "waterproof", "fireproof" ] ] }
  10. }])
  • Field is not equal to null
  • If a given field is an array in any document in the collection,wildcard indexes cannot support queries for documents where thatfield is not equal to null.

For example, consider a collection inventory with a wildcardindex on product_attributes. The wildcard indexcannot support the following queries ifproduct_attributes.tags is an array in any document in thecollection:

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

Sharding

You cannot shard a collection using a wildcard index. Create anon-wildcard index on the field or fields you want to shard on.For more information on shard key selection, seeShard Keys.