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:
- db.inventory.find( {"product_attributes" : { $exists : false } } )
- db.inventory.aggregate([
- { $match : { "product_attributes" : { $exists : false } } }
- ])
- 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:
- db.inventory.find({ "product_attributes" : { "price" : 29.99 } } )
- db.inventory.find({ "product_attributes.tags" : [ "waterproof", "fireproof" ] } )
- db.inventory.aggregate([{
- $match : { "product_attributes" : { "price" : 29.99 } }
- }])
- db.inventory.aggregate([{
- $match : { "product_attributes.tags" : ["waterproof", "fireproof" ] } }
- }])
- 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:
- db.inventory.find( { $ne : [ "product_attributes", { "price" : 29.99 } ] } )
- db.inventory.find( { $ne : [ "product_attributes.tags", [ "waterproof", "fireproof" ] ] } )
- db.inventory.aggregate([{
- $match : { $ne : [ "product_attributes", { "price" : 29.99 } ] }
- }])
- db.inventory.aggregate([{
- $match : { $ne : [ "product_attributes.tags", [ "waterproof", "fireproof" ] ] }
- }])
- 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:
- db.inventory.find( { $ne : [ "product_attributes.tags", null ] } )
- db.inventory.aggregate([{
- $match : { $ne : [ "product_attributes.tags", null ] }
- }])
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.